admin 管理员组文章数量: 1086019
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.
2 Answers
Reset to default 7Using 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);
本文标签: javascriptSignalR connect to multiple serversStack Overflow
版权声明:本文标题:javascript - SignalR connect to multiple servers - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744092075a2532222.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论