Javascript Socket.io 指定心跳
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31119691/
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
Socket.io specifying heartbeat
提问by Kasper Rynning-T?nnesen
I can't seem to find out how to set the heartbeat options for socket.io? I'd love to be able to specify what happens when a "timemout" happens, and when the server hasn't gotten any response from the client for some time.
我似乎不知道如何为 socket.io 设置心跳选项?我希望能够指定当“超时”发生时会发生什么,以及服务器在一段时间内没有从客户端得到任何响应时会发生什么。
For some reason the disconnect event isn't always firing, even though it has passed over an hour without the client not being connected. This often happens with the standalone Java socket.io client
由于某种原因,断开连接事件并不总是触发,即使它已经过去了一个多小时而没有连接客户端。这经常发生在独立的Java socket.io 客户端上
回答by Tholle
Socket.IO uses engine.io, which allows you to specify ping interval and ping timeout, like so:
Socket.IO 使用 engine.io,它允许您指定ping interval 和 ping timeout,如下所示:
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http, {'pingInterval': 2000, 'pingTimeout': 5000});
(2 second interval and 5 second timeout is just an example. It might be to frequent for your use case)
(2 秒间隔和 5 秒超时只是一个例子。对于您的用例来说可能很频繁)
回答by WiRa
For me the following worked:
对我来说,以下工作有效:
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
io.set('heartbeat timeout', 4000);
io.set('heartbeat interval', 2000);
For package dependency versions:
对于包依赖版本:
"express": "^4.12.3",
"socket.io": "1.0"