SignalR 在客户端处理超时事件

SignalR handle timeout event on client side

我是 SignalR 的新手,我有一个网络应用程序可以使用这种技术与服务器通信(恕我直言,这非常酷)。

我想知道的是 - 有没有办法在客户端使用 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