websocket - 使用 websockets 和 SocketIO 防止 'heartbeat timeout'

标签 websocket socket.io connection-timeout

我的 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/

相关文章:

spring - 由于 Tomcat ServerContainer,Spring-Websocket 的 ClassCastException

ios - Socket.io 与 PHP websocket 握手?

php - Javascript 连接到 websocket

php - 如何在 MAMP 或 XAMPP 上安装 Ratchet WebSockets for PHP?

node.js - 为什么我的 socket.io 事件无法在多个浏览器上处理?

php - 计算机锁定时 MySQL 连接不工作

php - MySQL 5.6 需要很长时间才能连接,即使是 "MySQL 5.6 Command Line Client"

node.js - expressjs 和 socket.io - 客户端上的重复消息

angularjs - Angular 1.6.9 和 socket.io 聊天应用程序

java - 在我的应用程序上执行 Ant 构建时出现 java.net.ConnectException