【问题标题】:SignalR: Sometimes Negotiating requests returns 404 error (Load-balanced)SignalR:有时协商请求会返回 404 错误(负载平衡)
【发布时间】:2015-05-22 08:22:52
【问题描述】:

我正在使用 signalR 2.1.2,我正在向我的客户推送通知。 前端是负载平衡的。 奇怪的是:有时有效,有时无效。似乎与浏览器的类型没有任何关系。有时它可以在 IE 中运行,有时它可以在 Chrome 中运行。首先我认为传输类型可能是问题,这就是为什么我将传输类型降级为长轮询,不幸的是它没有帮助:

客户端:

$.connection.hub.logging = true; // Enable SignalR logging

$.connection.hub.start({ transport: 'longPolling' }).done(function() {
     console.log("Connected, using transport method: " + $.connection.hub.transport.name);

        }).fail(function() {
            console.log("ERROR! Could not establish SignalR connection");
        });


        $.connection.hub.stateChanged(function(change) {

            if (change.newState === $.signalR.connectionState.disconnected) {
                console.log("State of SignalR connection changed to disconnected");                                       
            } else if (change.newState === $.signalR.connectionState.connected) {
                console.log("State of SignalR connection changed to connected");
            }
        });

$.connection.MessageHub.client.newIncoming = function(id) {
                // server call and that stuff
            }

服务器端:

[HubName("MessageHub")]
    public class MessageNotificationHub : Hub
    {
        // Methods that are implemented here, can be called by the client via ajax and broadcast to every other client

        public override Task OnConnected()
        {
            return base.OnConnected();
        }

    }

在服务器上:

public void Notify(long id)
{
      var notifyHubContext = GlobalHost.ConnectionManager.GetHubContext<MessageNotificationHub>();
            notifyHubContext.Clients.All.newIncoming(id);
}

没什么大不了的,只是看起来像另一个 signalR 样本。

以下是日志中的错误消息:

SignalR:客户端订阅了集线器“messagehub”。 SignalR:正在与 '/applicationname/signalr/negotiate?clientProtocol=1.4&connectionData=%5B%7B%22name%22%3A%22messagehub%22%...进行协商...

错误!无法建立 SignalR 连接

SignalR:停止连接。

SignalR 连接状态更改为断开连接

解码 URI 显示: https://server/applicationname/signalr/negotiate?clientProtocol=1.4&connectionData=[{"name":"messagehub"}]&_=1426756354570

有没有可能是负载均衡部分导致了我所有的麻烦。

感谢您的帮助! 谢谢

【问题讨论】:

    标签: asp.net signalr


    【解决方案1】:

    我发现问题在于 web.config 中缺少机器密钥。因此,Webserver A 创建了一个不同于 Webserver B 的机器密钥。 SignalR 使用机器密钥来验证消息是否来自有效的发件人。 在我在 web.config 中添加机器密钥后,部署它并关闭所有会话,它就像魅力一样工作!

    【讨论】:

      猜你喜欢
      • 2016-03-01
      • 1970-01-01
      • 2012-04-25
      • 1970-01-01
      • 1970-01-01
      • 2016-09-25
      • 2021-04-28
      • 2013-11-23
      • 1970-01-01
      相关资源
      最近更新 更多