SignalR:检测客户端的连接状态 [英] SignalR: detect connection state on client

查看:687
本文介绍了SignalR:检测客户端的连接状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过你怎么可以捕获通过结合.disconnect事件与Sig​​nalR客户端断开事件。



现在,我已经做到了这一点我希望把客户变成了等待重新循环它不断尝试连接,直到成功或者用户取消了。是否枢纽暴露连接状态的财产?我想类似的信息(伪代码)

  VAR isConnected; 

功能onConnected(){isConnected = TRUE; }

hub.disconnect =功能(){而(hub.notconnected){连接(); }


解决方案

在JS客户端尝试重新连接一定时间期间,默认为110秒。您可以订阅connection.stateChanged事件,并获得在状态更改,这样就可以把它显示给用户,或验证SignalR的不同场景断线响应上的更新。



< 。p>在我的测试中,国家已正确更新,以断开并重新连接等,正如你所期望



更多关于signalr连接


$ b信息$ b

 函数connectionStateChanged(州){
变种stateConversion = {0:'连接',1:连接,2:重新连接,4:'断开'};
的console.log(+ stateConversion [state.oldState]
+'到:'SignalR状态更改从'+ stateConversion [state.newState]);
}

连接= $■连接(signalR_Endpoint);
connection.stateChanged(connectionStateChanged);
connection.start({waitForPageLoad:假});


I've seen how you can trap a disconnection event on the client side with SignalR by binding to the .disconnect event.

Now that I've done this, I want to put the client into a "waiting to reconnect cycle" where it continually tries to connect until it succeeds or the user cancels out. Does the hub expose a connection state property? I'm thinking something like (pseudo code)

var isConnected;

function onConnected() { isConnected = true; }

hub.disconnect = function() { while(hub.notconnected) { connect(); }

解决方案

The JS client attempts to reconnect for a certain time period, which defaults to 110 seconds. You can subscribe to the connection.stateChanged event, and get updates on when the state changes so that you can display it to the user, or validate SignalR's response to different disconnection scenarios.

In my testing, the state was correctly updated to disconnected and reconnecting etc., as you would expect.

More information on signalr connections

function connectionStateChanged(state) {
    var stateConversion = {0: 'connecting', 1: 'connected', 2: 'reconnecting', 4: 'disconnected'};
    console.log('SignalR state changed from: ' + stateConversion[state.oldState]
     + ' to: ' + stateConversion[state.newState]);
}

connection = $.connection(signalR_Endpoint);
connection.stateChanged(connectionStateChanged);
connection.start({ waitForPageLoad: false });

这篇关于SignalR:检测客户端的连接状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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