SignalR: Sometimes Negotiating requests returns 404 error (Load-balanced)

白昼怎懂夜的黑 提交于 2019-12-08 13:31:06

问题


I am using signalR 2.1.2 and I am pushing notifications to my client. The front end is load balanced. The weird thing is: Sometimes it works and sometimes it doesn't. It seems that it doesn't have anything to do with the type of browser. Sometimes it works in IE sometimes it works in Chrome. First I thought that the type of transport might be the problem, that's why I downgraded the transport type to long polling, unfortunately it didn't help:

Client Side:

$.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
            }

Server Side:

[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();
        }

    }

On Server:

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

No big deal, just looks like another signalR sample.

Here are the error messages from the log:

SignalR: Client subscribed to hub 'messagehub'. SignalR: Negotiating with '/applicationname/signalr/negotiate?clientProtocol=1.4&connectionData=%5B%7B%22name%22%3A%22messagehub%22%...

ERROR! Could not establish SignalR connection

SignalR: Stopping connection.

State of SignalR connection changed to disconnected

Decoding the URI shows: https://server/applicationname/signalr/negotiate?clientProtocol=1.4&connectionData=[{"name":"messagehub"}]&_=1426756354570

Is it possible that the load balancing part causes all my troubles.

I appreciate your help! Thx


回答1:


I found out, that the problem was the missing machine key in the web.config. Hence, the Webserver A created another machine key than webserver B has. SignalR uses the machine key to verify if the message is from a valid sender. After I added the machine key in the web.config, deployed it and closed all sessions it worked like charm !



来源:https://stackoverflow.com/questions/29140895/signalr-sometimes-negotiating-requests-returns-404-error-load-balanced

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!