gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 10:38:17 24 4
gpt4 key购买 nike

我是 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

关于javascript - SignalR 在客户端处理超时事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33316878/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com