使用websockets和SocketIO防止“心跳超时”

时间:2013-12-04 22:35:01

标签: websocket socket.io connection-timeout

我使用NodeJS和SocketIO作为我的websocket解决方案。它工作正常,但几分钟后,我的套接字服务器总是在我的控制台中超时以及以下消息:

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!');
  });

如何防止超时发生?

1 个答案:

答案 0 :(得分:6)

查看关闭超时心跳超时选项here

您可以通过以下方式在服务器上以编程方式设置这些:

var io = require('socket.io').listen(80);
io.set('close timeout', 60);
io.set('heartbeat timeout', 60);

至于您的应用程序的设计,您应该检查this是否应该更改超时的问题。