如何让SignalR表现优雅断线的情况下, [英] How to make SignalR behave gracefully in case of disconnection

查看:1671
本文介绍了如何让SignalR表现优雅断线的情况下,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序通常会被使用在性交企业网络(代理,防火墙等),所以我不能靠插座和长轮询几乎是我唯一的选择。这是我最大的问题与长轮询:

My application will often be used under fucked up corporate networks (proxy, firewalls, etc.) so I can not rely on sockets and long-polling is practically my only option. Here is my biggest problem with long-polling:


  1. 长轮询是在一定的时间点断开

  2. 我自动重新连接(accoring到文档)

  3. 用户点击的东西,导致中枢方法调用,但是未建立连接尚未 - 错误发生

有什么办法同步枢纽方法调用并重新连接,如:

Is there any way to synchronize hub method call and reconnection like:


  1. 我称之为枢纽法

  2. SignalR看到连接还没有准备好

  3. ,它等待,直到连接已准备就绪,毕竟调用一个方法枢纽

现在我没有看到这样的oportunity而且好像我唯一的选择是切换到ajax调用这是可悲的。

For now I don't see such an oportunity and it seems like my only option is to switch to ajax calls which is sad.

推荐答案

我该如何对付它:


  • 有一个 isConnected布尔我的客户上,这样每当我联系,我将它设置为真正 ,每当我断开我把它设回

  • have a isConnected boolean on my client so that whenever I connect, I set it to true, and whenever I disconnect I set it back to false:

$ connection.hub.start()失败((ERR)=> {警报(ERR);})。完成(()=> this.isConnected = TRUE);
$ .connection.hub.disconnected(()=> {this.isConnected = FALSE;});
$ .connection.hub.reconnected(()=> {this.isConnected = TRUE;});

现在,之前,我拨打电话到集线器的方法,我检查 isConnected 是真实的。如果不是这样,我开始尝试枢纽连接:

now, right before I make the call to a hub method, I check if isConnected is true. If not, I attempt start the hub connection:

$(#serverButton)。点击(函数(){
    如果(this.isConnected == FALSE)
        $ .connection.hub.start();
    $ .connection.hub.server.serverMethod();结果
})

这是我现在如何处理这种情况。
希望这可以帮助。祝你好运!

This is how I handle such situations right now. Hope this helps. Best of luck!

更新:目前确实存在于 $属性connection.hub 名为状态,您可以访问每在连接期间点。

UPDATE: There actually exists a property on $.connection.hub called state that you can access at every point during the connection.

所以,你可以通过检查跳过拥有自己变的步骤(虽然我仍然使用它只是要确定) $。connection.hub.state

So you can skip the step of having your own variable (though I still use it just to be sure) by checking $.connection.hub.state.

$。connection.connectionState
对象{连接:0,连接:1,重新连接:2,断线:4}

$输出connection.hub.state 从上面的那些整数,但你可以做这样的事情: $。 connection.hub.state == $ .signalR.connectionState.connected ,你可以有布尔值。

The output from $.connection.hub.state is an integer from the ones above, but you can do something like this: $.connection.hub.state == $.signalR.connectionState.connected and you can have booleans.

更容易,但你仍然有这个您对枢纽方法的调用每次检查。

Easier, but you still have to check this every time you make a call to a hub method.

这篇关于如何让SignalR表现优雅断线的情况下,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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