【问题标题】:Why does my web page lose the ability to receive signalr messages为什么我的网页无法接收信号器消息
【发布时间】:2012-05-03 16:49:42
【问题描述】:

基本上我有 3 个应用程序。用于我的集线器的 Web 应用程序、Nservicebus 和信号器自托管服务器。我目前正在使用信号器版本 .4。基本上,Web 应用程序会在页面加载时启动与自托管服务器的连接。用户执行向服务总线发送命令的操作。然后服务总线连接到信号器服务器以通知所有 Web 客户端。

我的网页:

<script src="http://localhost:8081/test/signalr/hubs" type="text/javascript"></script>
<script>
    jQuery.support.cors = true;
    var myHub;  

    $.connection.hub.url = 'http://localhost:8081/test/signalr/hubs';

    $(function () {
        myHub = $.connection.userAccountHub;

        myHub.ChangeNameUpdated = function (connId) {
            alert("recieved signalr message");          
        };

        $.connection.hub.start().done(function() {
            var myClientId = $.connection.hub.id;
            setCookie("srconnectionid", myClientId);
        });
    });

</script>

我的 SignalR 服务器:

      static void Main(string[] args)
    {
        Debug.Listeners.Add(new ConsoleTraceListener());
        Debug.AutoFlush = true;

        string url = "http://localhost:8081/test/";
        var server = new Server(url);


        server.EnableHubs();
        server.Start();


        Console.WriteLine("Server running on {0}", url);

        while (true)
        strong text{
            ConsoleKeyInfo ki = Console.ReadKey(true);
            if (ki.Key == ConsoleKey.X)
            {
                break;
            }
        }
    }

我在 Signalr 服务器项目中的 Hub

 public class UserAccountHub : Hub
    {

        public void UsersNameChanged(string passedThroughConnectionId)
        {
            Clients.ChangeNameUpdated(passedThroughConnectionId);
        }
    }

来自我的 nservicebus 的电话

        //connect to signalRServer
        var connection = new HubConnection("http://localhost:8081/test/signalr/hubs");
        IHubProxy myHub = connection.CreateProxy("SignalRServer.Hubs.UserAccountHub");

        //Credentials are inherited from the application
        connection.Credentials = CredentialCache.DefaultNetworkCredentials;

        var test = message.GetHeader("signalrConnectionId");


        connection.Start().Wait();
        myHub.Invoke("UsersNameChanged", message.GetHeader("signalrConnectionId")).ContinueWith(task =>
        {
            //for debuging purposes
            if (task.IsFaulted)
            {
                Console.WriteLine(
                    "An error occurred during the method call {0}",
                    task.Exception.GetBaseException());
            }
            else
            {
                Console.WriteLine("Successfully called MethodOnServer");
            }
        });

我打开了 2 个浏览器。我在第二个浏览器上启动该过程,只有第一个浏览器收到通知。第二个浏览器没有。

我在运行 fiddler 时也没有发现任何突出的问题。

有没有更好的方法来实现这个实现,还是我遗漏了什么?

【问题讨论】:

  • 这些都有效吗? var connection = new HubConnection("http://localhost:8081/test/signalr/hubs"); 上面这行不正确。您像这样创建与集线器 url 的连接:var connection = new HubConnection("http://localhost:8081/test/"); 与您的 javascript 集线器 url 相同,它应该是:$.connection.hub.url = 'localhost:8081/test/signalr';
  • 它“有时”工作,这让我觉得我在做正确的连接部分。如果我只使用一个浏览器,一切似乎都可以正常工作(第一次),如果我尝试执行第二个操作,它将停止接收通知。但是,如果我从头开始刷新页面,它将收到 1 个通知,然后不再收到。在我拥有的另一个项目上查看提琴手后,我看到的似乎有所不同。此解决方案在收到第一个通知后停止长轮询。
  • 我将使用您指定的连接进行检查。
  • 这很奇怪。看起来它根本不应该工作。
  • 感谢大卫,您的 cmets 让我朝着正确的方向前进。我升级到 .5,然后仔细研究了我是如何处理连接字符串的。

标签: signalr


【解决方案1】:

好的!大卫的 cmets 让我朝着正确的方向前进。还注意到这是更新的,以前没有注意到这一点。 Scroll to the bottom for the cross domain support

1.) 更新到版本 .5

2.) 将我在 javascript 中的连接信息修改为

$.connection.hub.url = 'http://localhost:8081/test/signalr';

$.connection.hub.start({ transport: 'longPolling', xdomain: true }).done(function () {
    //alert("staring hub connection:  " + $.connection.hub.id);
    setCookie("srconnectionid", $.connection.hub.id);
});

3.) 我不得不更改HubConnection

var connection = new HubConnection("http://localhost:8081/test/");

4.) 我不得不更改 IHubProxy

IHubProxy myHub = connection.CreateProxy("UserAccountHub");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-28
    • 2010-11-21
    • 1970-01-01
    相关资源
    最近更新 更多