0


更新4:(已解决

  • 做了新项目;
  • 进口来源;
  • 将 SignalR 更新到最新版本(通过 NuGet 包管理器控制台);
  • 做了相应的修改(根据官方文档:http ://www.asp.net/signalr/overview/signalr-20/hubs-api/hubs-api-guide-server )。

最后,一切都像魅力一样运作。感谢 SignalR 团队!


这个 SignalR 做得很好,迫不及待想试试!

我找到了本教程 - SignalR - 5 分钟演示 ( http://www.youtube.com/watch?v=tEBaDo_sFfA )

但是,我有一些问题: - 我安装了 VS2012Ultimate,在创建示例 ASP.NET MVC4 Internet 应用程序后,右键单击参考 -> 管理 NuGet 包,搜索 SignalR .... 没有像视频中那样出现,没有那些:SignalR、SignalR.Js、SignalR.Server ... :( 有谁知道为什么?

UPDATE1:我安装了 SignalR(http://nuget.org/packages/microsoft.aspnet.signalr),之后,我安装了 asp.net 秋季更新(http://www.microsoft.com/en-us/download /details.aspx?id=35493)。

但是我从下面得到关于函数“ writeMessage”的错误ChatHub.cs

"Error1'Microsoft.AspNet.SignalR.Hubs.HubConnectionContext' does not contain a definition for 'writeMessage'
and no extension method 'writeMessage' accepting a first argument of type
'Microsoft.AspNet.SignalR.Hubs.HubConnectionContext' could be found (are you missing a using directive or an
assembly reference? ..\MvcApplication1\Hubs\ChatHub.cs"

Clients.All.在聊天初始化和使用之后使用orClients.Others.和启动集线器“解决”此错误后chat.writeMessage,在 GoogleChrome 控制台中加载时出现两个错误:

  • Index.cshtml中:

    "Uncaught TypeError: Cannot set property 'writeMessage' of undefined"
    
  • localhost:3420/signalr/hubs

    "Uncaught TypeError: Object #<Object> has no method 'createProxy'"
    
    • 在这条线上

      // Create and store the hub proxy
          hub._.proxy = hubConnection.createProxy(hub._.hubName);
      

Clients.All.在使用orClients.Others.并在最后启动集线器并使用chat.client.writeMessage(我安装了 Microsoft.AspNet.SignalR.Client)“解决”此错误后,在 GoogleChrome 控制台中加载时出现两个错误:

  • Index.cshtml中:

    "Uncaught TypeError: Cannot read property 'client' of undefined"
    

任何人都可以帮助我吗?:(

提前致谢!!

文件 ChatHub.cs

namespace MvcApplication1.Hubs
{
    //[Microsoft.AspNet.SignalR.Hubs.HubName("chathub")]
    [Microsoft.AspNet.SignalR.Hubs.HubName("chatHub")]
    public class ChatHub : Microsoft.AspNet.SignalR.Hubs.Hub
    {
        public void BroadcastMessage(string message)
        { 
            //rebroadcast the message to all the connected clients
            Clients.writeMessage(message);//ERROR on compile

        //I tried both of the followings, give no error, BUT when i press Submit doesn't appear messages anywhere
            //Clients.All.writeMessage(message);
            //Clients.Others.writeMessage(message);
        }
    }
}

文件 Index.cshtml

@section scripts
{
    <script type="text/javascript" src="~/Scripts/jquery.signalR-1.0.0-alpha2.js"></script>

@*not working, GET localhost:3420/SignalR 500 (Internal Server Error)*@
    @*<script type="text/javascript" src="SignalR"></script>*@

@*  with any of these declarations i don't get 500 error any more, but still not working *@
    <script type="text/javascript" src="~/signalr/hubs"></script>
    @*<script type="text/javascript" src="../signalr/hubs"></script>*@    

    <script type="text/javascript">
        $(document).ready(function()
        {
        //i tried these, but in vain..
            //var connection = new Microsoft.AspNet.SignalR.Connection("localhost:3420/");
            //$.connection.hub.url = 'localhost:3420/signalr/';

            var chat = $.connection.chatHub;

            $.connection.hub.start();

        //Uncaught TypeError: Cannot set property 'writeMessage' of undefined 
            //chat.writeMessage = function (msg)

        //Uncaught TypeError: Cannot read property 'client' of undefined 
            chat.client.writeMessage = function (msg) {
            {
                $("#messages").append("<li>" + msg + "</li>");
            }

            $("#buttonSubmit").click(function ()
            {
                //chat.broadcastMessage($("#txtInput").val());
                chat.server.broadcastMessage($("#txtInput").val());
            });

            $.connection.hub.start();//just one error at client
        });
    </script>
}
<h5>Chat with SignalR</h5>       
<fieldset>
    <legend>SignalR Demo</legend>
    <label for="txtInput">Chat Message</label>
    <input id="txtInput" type="text" />
    <button id="buttonSubmit">Submit</button>            
    <fieldset>
        <legend>Messages</legend>
        <ul id="messages"></ul>
    </fieldset>
</fieldset>

更新2

根据Tim B James的建议,我:

提及:

- 将函数中的代码Main放入我的HomeController.cs-> 函数中:public ActionResult Index()

- 改变了这一行:

var hubConnection = new HubConnection("localhost/mysite");

有了这个:

var hubConnection = new HubConnection("localhost:14079");

但是,我的页面没有加载(“等待本地主机...”)

- 如果我不将 Main 中的代码放入控制器的函数中,

我收到此错误

    "Uncaught TypeError: Cannot read property 'chat' of undefined "
on this line: 

    var chat = $.connection.chat;

更新3

UPDATE2 与删除线线条

更新4:(已解决

开头的暗示。

4

2 回答 2

2

最新版本有一些未记录的细微差别,事实上我今天正在处理它们。

首先,在您引用的链接中,您不需要 Main 中的代码。这是为了在 WPF 应用程序或 Windows 8 应用程序中使用 Signalr。

第一个问题可能是也可能不是问题,是 HubNameAttribute 的使用,它可能需要也可能不需要。
如果没有 HubName,您的集线器名称应解析为 $.connection.chatHub

您已经处理的下一部分是每个集线器上的新命名空间。

chatHub.client - These are client defined methods, that the server may invoke.
chatHub.server - These are server defined methods, that the client may invoke.
chatHub.state - This is the state that gets sent to and from the server.

现在未捕获的错误是 JavaScript 中没有正确输出的东西,很可能是集线器定义,因为集线器变量为空。您需要在后台验证集线器是否正常恢复。

<script type="text/javascript" src="~/signalr/hubs"></script>

这应该返回一个格式正确的 jQuery 就绪块,它定义了所有集线器和这些集线器的服务器方法。使用像Fiddler这样的工具将对此有所帮助。

我也会开始使用带 Firebug 的 Chrome 或 Firefox,它们各有优缺点,但它们都会为您提供网络监控和调试工具。能够捕获未处理的异常一直是尝试调试 JavaScript 的所有东西的救命稻草。

希望这可以帮助您解决悬而未决的问题。

于 2012-11-20T06:00:37.960 回答
0

首先,与现在可用的 SignalR 版本相比,YouTube 视频现在已经过时了。

其次,我会删除您正在使用的项目并重新开始。这次,通过 NuGet 包管理器安装 SignalR Alpha 版本。

安装包 Microsoft.AspNet.SignalR -pre

安装包 Microsoft.AspNet.SignalR.client -pre

完成此操作后,请阅读此处的 SignalR 网站上的文档/示例

https://github.com/SignalR/SignalR/wiki

如果您遵循一些基础知识,那么您应该进行排序。

于 2012-11-19T20:26:18.200 回答