javascript - SignalR connect to multiple servers - Stack Overflow

Is possible to connect using Javascript client to more than one SignalR servers?For example:<script

Is possible to connect using Javascript client to more than one SignalR servers?

For example:

<script type="text/javascript">
    $.connection.hub.url = '';
    var server1Hub = $.connection.server1Hub;

    $.connection.hub.start().done(function () {

    });

    // i need to connect to server2
    $.connection.hub.url = '';
    var server2Hub = $.connection.server2Hub;

    $.connection.hub.start().done(function () {

    });
</script>

Trying to connect (again) the second time gives me an error:

'server1Hub' Hub could not be resolved.

Can i create two instances of $.connection ? Obviously i think modifying the same connection can create many issues.

Is possible to connect using Javascript client to more than one SignalR servers?

For example:

<script type="text/javascript">
    $.connection.hub.url = 'http://server1/signalr';
    var server1Hub = $.connection.server1Hub;

    $.connection.hub.start().done(function () {

    });

    // i need to connect to server2
    $.connection.hub.url = 'http://server2/signalr';
    var server2Hub = $.connection.server2Hub;

    $.connection.hub.start().done(function () {

    });
</script>

Trying to connect (again) the second time gives me an error:

'server1Hub' Hub could not be resolved.

Can i create two instances of $.connection ? Obviously i think modifying the same connection can create many issues.

Share Improve this question asked Mar 30, 2015 at 12:58 CatalinCatalin 11.7k19 gold badges80 silver badges152 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

Using different $connection:

var connection1 = $.connection('/first');
connection1.start();

var connection2 = $.connection('/second');
connection2.start();

Subscribing on multiple hubs:

var connection1 = $.hubConnection("'http://server1/signalr");
var connection2 = $.hubConnection("http://server2/signalr");

var Hub1= connection1.createHubProxy('Hub1');
var Hub2= connection2.createHubProxy('Hub2');

connection1.start();
connection2.start();

Read more here: here at section Define method on client (without the generated proxy, or when adding after calling the start method)

Thank you SilentTremor. You saved me a lot of time.

If you are working with generated proxies then you can use something like:

var Hub1 = connection1.createHubProxies().<your hub class>

Replace with your own class. Then just get access to all server functions from Hub1. Example:

var Hub1 = connection1.createHubProxies().Chat;
...
Hub1.addMessage(name, message);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744092074a4557267.html

相关推荐

  • javascript - SignalR connect to multiple servers - Stack Overflow

    Is possible to connect using Javascript client to more than one SignalR servers?For example:<script

    9天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信