使用 websockets 和 SocketIO 防止“心跳超时" [英] Preventing a 'heartbeat timeout' with websockets and SocketIO

查看:124
本文介绍了使用 websockets 和 SocketIO 防止“心跳超时"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using NodeJS and SocketIO for my websocket solution. It works fine, but after a few minutes, my socket server always times out with the following messages in my console:

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 文件:

Here is my complete server.js file:

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

如何防止超时发生?

推荐答案

查看关闭超时心跳超时选项这里

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

You can set these programmatically on the server via:

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

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

As for the design of your application, you should check out this question for whether or not you should change the timeout.

这篇关于使用 websockets 和 SocketIO 防止“心跳超时"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆