信号器检查集线器是否已启动 [英] Signalr check if hub already started

查看:76
本文介绍了信号器检查集线器是否已启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个带有signalR功能的javascript块。



我不知道执行的顺序,所以我想用

$启动集线器b
$ b

$。connection.hub.start();



如果是尚未启动。



如何检查集线器是否已启动?多次启动它会引发错误。

解决方案

有几种方法可以解决这个问题。第一种是创建自己的连接状态跟踪变量,使用连接回调事件设置:

  $。connection.hub .start()。done(function(){ConnectionStarted = true;})

你可以检查ConnectionStarted在尝试启动连接之前。不幸的是,这不会很好,因为start()是异步的,所以很多实例可能会在一个完成之前尝试启动连接并将ConnectionStart设置为true。



所以,工作解决方案。那里有两个。首先,让每个实例都使用自己的连接对象(即:不要使用默认的$ .connection.hub,而是使用手动连接创建者:

  var localConnection = $ .hubConnection(); 
var localHubProxy = localConnection.createHubProxy('HubNameHere');

这不是很好,因为大多数浏览器每页允许的连接数量有限,而且因为它通常是矫枉过正。



< IMO,最好的解决方案是使用默认代理($ .connection.hub)的单一自动连接,并查看连接状态(我刚刚遇到的)。每个连接对象都有一个状态:

  $。signalR.connectionState 
Object {connected:0,connected:1,reconnecting:2,disconnected:4}

那么,在每个例子中,去找这样的东西?:

  if($ .connection.hub&& $ .connection.hub.state === $ .signalR.connection State.disconnected){
$ .connection.hub.start()
}

另请注意,当您创建连接时,它将处于已断开连接状态/ 4,直到调用它为止。一旦调用start,连接显然会尝试不断重新连接(如果它被中断),直到$ .connection.hub.stop()被调用(然后将返回状态断开连接)。



参考:



http://www.asp.net/signalr/overview/hubs-api/hubs-api-guide-javascript-client#establishconnection
https://github.com/SignalR/SignalR/wiki


I have multiple javascript blocks with signalR functions.

I don't know the order of execution so that i want to start the hub with

$.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.

解决方案

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.

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:

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:

$.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").

Refs:

http://www.asp.net/signalr/overview/hubs-api/hubs-api-guide-javascript-client#establishconnection https://github.com/SignalR/SignalR/wiki

这篇关于信号器检查集线器是否已启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆