SignalR: Sometimes Negotiating requests returns 40

2019-06-12 19:54发布

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条回答
家丑人穷心不美
2楼-- · 2019-06-12 20:27

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 !

查看更多
登录 后发表回答