最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - SignalR - multiple hubs - start connections individually - Stack Overflow

programmeradmin1浏览0评论

I have a SignalR application which has multiple hubs (the hubs are part of different projects inside the same solution).

In the front-end I want to start connections based on the ponent a user is currently viewing.

Let's assume I have 2 hubs and 2 ponents: TestHub1, TestHub2; Component1, Component2.

In each ponent I instantiate the connection as following:

var testHub = $.connection.testHub;

            //define client methods

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

So I do this in multiple ponents. Now, assuming I have both ponents connected to TestHub1 and TestHub2 respectively (at the same time), how can I only stop one connection? If in any ponent I call $.connection.hub.stop(), both hub connections are stopped.

How can I start and stop the hub connections individually? (Because if at one point after I have stopped both of them and I call $.connection.hub.start(), even if I call this from the ponent which uses TestHub1, TestHub2 will also start the connection.

So I am looking for a way to start and stop individual hub connections rather that the entire $.connection.start() and $.connection.hub.stop().

Thanks!

I have a SignalR application which has multiple hubs (the hubs are part of different projects inside the same solution).

In the front-end I want to start connections based on the ponent a user is currently viewing.

Let's assume I have 2 hubs and 2 ponents: TestHub1, TestHub2; Component1, Component2.

In each ponent I instantiate the connection as following:

var testHub = $.connection.testHub;

            //define client methods

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

So I do this in multiple ponents. Now, assuming I have both ponents connected to TestHub1 and TestHub2 respectively (at the same time), how can I only stop one connection? If in any ponent I call $.connection.hub.stop(), both hub connections are stopped.

How can I start and stop the hub connections individually? (Because if at one point after I have stopped both of them and I call $.connection.hub.start(), even if I call this from the ponent which uses TestHub1, TestHub2 will also start the connection.

So I am looking for a way to start and stop individual hub connections rather that the entire $.connection.start() and $.connection.hub.stop().

Thanks!

Share Improve this question asked Sep 25, 2015 at 7:30 radu-mateiradu-matei 3,5201 gold badge32 silver badges47 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

Default generated proxy class creates one connection for all hubs. So you can have multiple hubs sharing one connection on your site. But all Hubs get the same HTTP request information. http://www.asp/signalr/overview/guide-to-the-api/hubs-api-guide-server#multiplehubs

Since all Hubs share the same connection, the only HTTP request information that the server gets is what es in the original HTTP request that establishes the SignalR connection. If you use the connection request to pass information from the client to the server by specifying a query string, you can't provide different query strings to different Hubs. All Hubs will receive the same information.

To manage connections for each hub manualy you need to implement proxy by yourself:

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

Please look here to find out more details http://www.asp/signalr/overview/guide-to-the-api/hubs-api-guide-javascript-client#getproxy

发布评论

评论列表(0)

  1. 暂无评论