javascript Signalr 检查集线器是否已经启动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15066693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Signalr check if hub already started
提问by daniel
I have multiple javascript blocks with signalR functions.
我有多个带有signalR 函数的javascript 块。
I don't know the order of execution so that i want to start the hub with
我不知道执行顺序,所以我想启动集线器
$.connection.hub.start();
$.connection.hub.start();
if it isn't started already.
如果它还没有开始。
How can i check if the hub is already started? Starting it multiple times it it throws an error.
如何检查集线器是否已启动?多次启动它会引发错误。
回答by dpb
There are a few ways to approach this problem. The first is to create your own connection status tracking variables, which you set with the connection callback events:
有几种方法可以解决这个问题。第一个是创建您自己的连接状态跟踪变量,您可以使用连接回调事件设置这些变量:
$.connection.hub.start().done(function() { ConnectionStarted = true; })
You can check ConnectionStarted before attempting to start the connection. Unfortunately, this won't work well, as start() is asynchronous and so many instances could try to start a connection before one has finished and set ConnectionStart to true.
您可以在尝试启动连接之前检查 ConnectionStarted。不幸的是,这不会很好地工作,因为 start() 是异步的,因此许多实例可能会在连接完成之前尝试启动连接并将 ConnectionStart 设置为 true。
So, working solutions. There are two. First, have every instance use its own connection object (ie: don't use the default $.connection.hub, but instead use manual connection creator:
所以,工作解决方案。那里有两个。首先,让每个实例使用自己的连接对象(即:不要使用默认的 $.connection.hub,而是使用手动连接创建者:
var localConnection = $.hubConnection();
var localHubProxy= localConnection.createHubProxy('HubNameHere');
This isn't great, as most browsers have a limited number of connections allowed per page, and also because it is generally overkill.
这不是很好,因为大多数浏览器每个页面允许的连接数量有限,而且通常是矫枉过正。
IMO, best solution is to use the single automatic connection with default proxy ($.connection.hub) and look at the connection state (something I just came across). Each connection object has a state:
IMO,最好的解决方案是使用带有默认代理($.connection.hub)的单一自动连接并查看连接状态(我刚刚遇到的东西)。每个连接对象都有一个状态:
$.signalR.connectionState
Object {connecting: 0, connected: 1, reconnecting: 2, disconnected: 4}
So, in each instance, go for something like this?:
因此,在每种情况下,请执行以下操作?:
if ($.connection.hub && $.connection.hub.state === $.signalR.connectionState.disconnected) {
$.connection.hub.start()
}
Also note that when you create a connection, it will sit in state "disconnected" / 4 until start is called on it. Once start is called, the connection will apparently try to reconnect constantly (if it is interrupted) until $.connection.hub.stop() is called (will then go back to state "disconnected").
另请注意,当您创建连接时,它将处于“已断开连接”/ 4 状态,直到对其调用 start。一旦 start 被调用,连接显然会不断尝试重新连接(如果它被中断),直到 $.connection.hub.stop() 被调用(然后将返回到“断开连接”状态)。
Refs:
参考:
http://www.asp.net/signalr/overview/hubs-api/hubs-api-guide-javascript-client#establishconnectionhttps://github.com/SignalR/SignalR/wiki
http://www.asp.net/signalr/overview/hubs-api/hubs-api-guide-javascript-client#establishconnection https://github.com/SignalR/SignalR/wiki
回答by LiamB
You can detect when the hub has started using .done()
您可以使用 .done() 检测集线器何时开始
$.connection.hub.start().done(function () {
});
using this method, you can do the following (Taken from docs : https://github.com/SignalR/SignalR/wiki/SignalR-JS-Client-Hubs) you can then keep track of if the connection is open yourself.
使用此方法,您可以执行以下操作(取自文档:https: //github.com/SignalR/SignalR/wiki/SignalR-JS-Client-Hubs)然后您可以跟踪自己是否打开了连接。
function connectionReady() {
alert("Done calling first hub serverside-function");
};
$.connection.hub.start()
.done(function() {
myHub.server.SomeFunction(SomeParam) //e.g. a login or init
.done(connectionReady);
})
.fail(function() {
alert("Could not Connect!");
});
回答by Owen Pauling
You can check the connection state in each of your functions like:
您可以在每个函数中检查连接状态,例如:
function doSomething {
if ($.connection.hub.state === $.signalR.connectionState.disconnected) {
$.connection.hub.start().done(function () { myHub.server.myHubMethod(); });
}
else {
myHub.server.myHubMethod();
}
}