使用websockets和SocketIO防止"心跳超时"

 mobiledu2502922957 发布于 2023-02-12 18:56

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

debug - fired heartbeat timeout for client
info - transport end 
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 个回答
  • 在此处查看关闭超时心跳超时选项

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

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

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

    2023-02-12 18:58 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有