我的 websocket 解决方案使用 NodeJS 和 SocketIO。它工作正常,但几分钟后,我的套接字服务器总是超时并在我的控制台中显示以下消息:
debug - fired heartbeat timeout for client
info - transport end <heartbeat timeout>
debug - set close timeout for client
debug - cleared close timeout for client
debug - discarding transport
这是我的完整
server.js
文件:var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
app.listen(3000);
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'from socket server' });
socket.on('swipe', function (from, msg) {
console.log('I received a private message by ', from, ' saying ', msg);
socket.emit('swipe event received on server!');
});
如何防止超时发生?
最佳答案
查看 关闭超时和 心跳超时 选项here
您可以通过以下方式在服务器上以编程方式设置这些:
var io = require('socket.io').listen(80);
io.set('close timeout', 60);
io.set('heartbeat timeout', 60);
至于你的应用程序的设计,你应该查看this是否应该更改超时的问题。
关于websocket - 使用 websockets 和 SocketIO 防止 'heartbeat timeout',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20387550/