Socket.io specifying heartbeat

2019-02-06 15:28发布

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.

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

2条回答
相关推荐>>
2楼-- · 2019-02-06 15:33

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"
查看更多
我命由我不由天
3楼-- · 2019-02-06 15:52

Socket.IO uses engine.io, which allows you to specify ping interval and ping timeout, like so:

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)

查看更多
登录 后发表回答