编程语言
首页 > 编程语言> > javascript-客户端的SignalR处理超时事件

javascript-客户端的SignalR处理超时事件

作者:互联网

我是SignalR的新手,我有一个网络应用程序,使用此技术(非常酷的IMHO)与服务器进行通信.

我想知道的是-有什么方法可以在客户端使用SingalR处理超时事件?是否存在我可以在客户端收听的超时事件?

当我超时时,我可以在他的日志中看到它:

[12:56:47 GMT+0300 (Jerusalem Daylight Time)] SignalR: Keep alive has been missed, connection may be dead/slow.
[12:56:54 GMT+0300 (Jerusalem Daylight Time)] SignalR: Keep alive timed out.  Notifying transport that connection has been lost.

为了清楚起见,我不想运行自己的计时器并检查是否发生超时,我想问的是SignalR中是否触发了这样的事件?

谢谢!

解决方法:

如您所见here $.connection.hub.disconnected

Handle the disconnected event to display a message when an attempt to
reconnect has timed out. In this scenario, the only way to
re-establish a connection with the server again is to restart the
SignalR connection by calling the Start method, which will create a
new connection ID. The following code sample uses a flag to make sure
that you issue the notification only after a reconnecting timeout, not
after a normal end to the SignalR connection caused by calling the
Stop method.

您可以通过$.connection.hub.disconnected事件处理程序来管理连接超时:

$.connection.hub.disconnected(function() {
    if(tryingToReconnect) {
    notifyUserOfDisconnect(); // Your function to notify user.
 }
});

如果使用的是长轮询连接,则应引起注意,因为如果网络连接突然断开,则不会调用OnDisconnected().

长轮询没有使客户端保持活动状态,并且浏览器本身可能要花费几分钟才能意识到连接确实死了.

Reference

标签:signalr,signalr-client,signalr-hub,asp-net,javascript
来源: https://codeday.me/bug/20191119/2036936.html