Socket.io 延迟触发“断开连接"事件? [英] Socket.io delay in firing the "disconnect" event?

查看:41
本文介绍了Socket.io 延迟触发“断开连接"事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个连接到 node.js 服务器的 socket.io 客户端.如果我在命令行中杀死 node.js,客户端会立即冻结(即,通信停止),但在触发断开连接"事件之前有大约 20 秒的延迟.这是设计使然吗?是否有配置选项可以减少触发断开连接事件的延迟?

I have a socket.io client connected to a node.js server. If I kill node.js at the command line, the client immediately freezes (i.e., communication stops), but there is a ~20 second delay before the "disconnect" event is fired. Is this behavior by design? Is there a configuration option to reduce the delay in firing the disconnect event?

这种行为似乎在 socket.io 的最近(最近 6 个月)更新中发生了变化.在 socket.io 本身内置重新连接功能之前,我使用断开连接"事件处理程序实现了自己的重新连接逻辑,当时当服务器通信停止时,断开连接"事件几乎立即触发.

It appears that this behavior changed in a relatively recent (last 6 months) update of socket.io. Before the reconnect functionality was built in to socket.io itself, I implemented my own reconnect logic using a "disconnect" event handler and at that time the "disconnect" event fired almost instantly when server communication halted.

推荐答案

我认为这可能是一种设计模式.客户端可能会假设服务器暂时"无法访问(网络流量等),并且基本上会继续尝试访问它...直到客户端超时.

I think this is likely a design pattern. The client may be presuming the server is 'temporarily' unreachable (network trafic etc) and essentially will keep trying to reach it... until the client timeout kicks in.

我直接从客户端向服务器发送断开连接 (socket.disconnect()),但我没有遇到这个问题.

I send a disconnect (socket.disconnect()) to the server directly from the client, and I don't get this issue.

这篇关于Socket.io 延迟触发“断开连接"事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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