javascript 非活动客户端连接的 socket.io 服务器端超时?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30991326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 13:05:07  来源:igfitidea点击:

socket.io server-side timeout for inactive client connection?

javascriptnode.jssocket.io

提问by jfriend00

I know that the socket.io client library will close the current socket.io connection (and then attempt to reconnect) if it is not regularly receiving a response to the ping packets that it sends to the server (under the assumption that the connection has died for some reason). And, there are client options for controlling this reconnect behavior.

我知道 socket.io 客户端库将关闭当前的 socket.io 连接(然后尝试重新连接),如果它没有定期收到对它发送到服务器的 ping 数据包的响应(假设连接有因某种原因死亡)。并且,有用于控制这种重新连接行为的客户端选项。

But, what happens server-side if a client goes inactive and stops sending ping messages (say because the client went to sleep)? I can't find any info in the socket.io server-side doc that explains that situation or allows for configuration of it. Will the server close an inactive client socket.io connection (one that it is not receiving ping messages from)? If so, how long will the server wait and is that behavior configurable?

但是,如果客户端处于非活动状态并停止发送 ping 消息(比如因为客户端进入睡眠状态),服务器端会发生什么?我在 socket.io 服务器端文档中找不到任何信息来解释这种情况或允许对其进行配置。服务器是否会关闭不活动的客户端 socket.io 连接(它没有接收到来自 ping 消息的连接)?如果是这样,服务器将等待多长时间以及该行为是否可配置?

回答by Vishnu

pingTimeout (Number): how many ms without a pong packet to consider the connection closed (60000)

pingInterval (Number): how many ms before sending a new ping packet (25000)

pingTimeout (Number): 多少毫秒没有 pong 包考虑连接关闭 (60000)

pingInterval (Number): 发送新的 ping 数据包前多少毫秒 (25000)

http://socket.io/docs/server-api/

http://socket.io/docs/server-api/

https://github.com/socketio/engine.io#methods-1

https://github.com/socketio/engine.io#methods-1

回答by Pi Da

As per the socket.io 2.20 readme (the latest version as of Jan 2020):

根据 socket.io 2.20 自述文件(截至 2020 年 1 月的最新版本):

"A heartbeat mechanism is implemented at the Engine.IO level, allowing both the server and the client to know when the other one is not responding anymore.

That functionality is achieved with timers set on both the server and the client, with timeout values (the pingIntervaland pingTimeoutparameters) shared during the connection handshake."

“在 Engine.IO 级别实现了心跳机制,允许服务器和客户端知道另一个何时不再响应。

该功能是通过在服务器和客户端上设置的计时器实现的,在连接握手期间共享超时值(pingIntervalpingTimeout参数)。”

So, you can configure both the server and client side timeout settings by setting the following timeout properties on the engine.io component inside of socket.io.

因此,您可以通过在 socket.io 内的 engine.io 组件上设置以下超时属性来配置服务器端和客户端超时设置。

Using socket.io version 2.20:

使用 socket.io 2.20 版:

const io = ...; // initialize socket.io how you wish
io.eio.pingTimeout = 120000; // 2 minutes
io.eio.pingInterval = 5000;  // 5 seconds

The same thing using older versions of socket.io:

使用旧版本的 socket.io 同样的事情:

const io = ...; // initialize socket.io how you wish
io.set('heartbeat timeout', 1200000);
io.set('heartbeat interval', 5000);