SignalR connect/disconnect hub blows up

2019-03-16 15:52发布

问题:

Also opened this as an issue here but hoping someone has seen this..

I've got a very simple hub that implements IConnected/IDisconnect. In a standalone project, this hub tested out great.

When I dropped it into my real project, where I already have some other hubs, adding it resulted in no hubs being available (confirmed that none show up in /signalr/hubs). I then commented out IConnected/IDisconnect in this hub and re-compiled, and it showed up along with the rest. Adding the interfaces back broke it all.

Has anyone seen this before? Is there some configuration missing or something?

public class ChatHub : Hub, IConnected, IDisconnect
{
    public void Test(string message)
    {

    }

    public System.Threading.Tasks.Task Connect(IEnumerable<string> groups)
    {
        this.Clients.onNewUserOnline(Context.ConnectionId);
        return new Task(() => { });
    }


    public Task Reconnect(IEnumerable<string> groups)
    {
        this.Clients.onNewUserOnline(Context.ConnectionId);
        return new Task(() => { });
    }

    public Task Disconnect()
    {
        this.Clients.onUserOffline(Context.ConnectionId);
        return new Task(() => { });
    }
}

回答1:

Here is a link to a similar question Is there a way to get number of connections in Signalr hub group?

If that didnt help you could look at the new Connection State stuff in SignalR 0.5.1

Connection State Changes

A SignalR connection has always been a black box to the user and this made it hard to detect changes in the underlying connection state. We’ve exposed a new stateChanged event in the JavaScript and .NET clients. This allows you to listen for state changes and react to them in different ways. Here’s an example that shows the user a message if the connection went into a reconnecting state and didn’t recover after 10 seconds:

var chat = $.connection.chat;
var timeout = null;
var interval = 10000;
chat.addMessage = function (msg) {
    $('#messages').append('<li>' + msg + '</li>');
};
$.connection.hub.stateChanged(function (change) {
    if (change.newState === $.signalR.connectionState.reconnecting) {
        timeout = setTimeout(function () {
            $('#state').css('backgroundColor', 'red')
                .html('The server is unreachable...');
        }, interval);
    } else if (timeout && change.newState === $.signalR.connectionState.connected) {
        $('#state').css('backgroundColor', 'green')
            .html('The server is online');
        clearTimeout(timeout);
        timeout = null;
    }
});
$.connection.hub.start();


标签: signalr