SignalR - Multiple Hubs - Run Connections Individually

I have an application SignalRthat has several hubs (hubs are part of different projects within the same solution).

In the interface, I want to start connections based on the component that the user is currently viewing.

Suppose I have 2 hubs and 2 components: TestHub1, TestHub2; Component1, Component2.

In each component, I create a connection as follows:

var testHub = $.connection.testHub;

            //define client methods

            $.connection.hub.logging = true;
            $.connection.hub.start();
        });

So, I am doing this in several components. Now, assuming that I have both components connected to TestHub1and TestHub2respectively (at the same time), how can I only stop one connection? If I call in any component $.connection.hub.stop(), both connections of the hub stop.

How can I start and stop hub connections individually? (Because if at some point after I stopped both of them, and I call $.connection.hub.start(), even if I call it from the component that uses TestHub1, TestHub2it will also start the connection.

So, I'm looking for a way to start and stop individual connections to the host, not integer $.connection.start()and $.connection.hub.stop().

Thanks!

+4
1

- . , , . HTTP. http://www.asp.net/signalr/overview/guide-to-the-api/hubs-api-guide-server#multiplehubs

, HTTP-, , - , HTTP-, SignalR. , , . .

-, - :

    var connection = $.hubConnection();
var contosoChatHubProxy = connection.createHubProxy('contosoChatHub');
contosoChatHubProxy.on('addContosoChatMessageToPage', function(name, message) {
    console.log(name + ' ' + message);
});
connection.start().done(function() {
    // Wire up Send button to call NewContosoChatMessage on the server.
    $('#newContosoChatMessage').click(function () {
        contosoChatHubProxy.invoke('newContosoChatMessage', $('#displayname').val(), $('#message').val());
        $('#message').val('').focus();
                });
    });

, , http://www.asp.net/signalr/overview/guide-to-the-api/hubs-api-guide-javascript-client#getproxy

+6

Source: https://habr.com/ru/post/1608942/


All Articles