简体   繁体   English

Socket.IO服务器的奇怪行为

[英]Strange behavior of Socket.IO server

My Socket.IO client starts making endless requests to server after few hours of working... 我的Socket.IO客户端在几个小时的工作后开始向服务器发出无尽的请求......

So. 所以。 There is the server that counts peoples online: 有一个服务器在线计算人们:

const http = require('http');
const io = require('socket.io');

const app = http.createServer();
const socket_io = io.listen(app);

app.listen(2053); 

let clients = [];
    socket_io.sockets.on('connection', (socket) => {
      let ip = socket.handshake.headers['x-real-ip'];
      if (ip && !clients.hasOwnProperty(ip)) {
        clients[ip] = 1;
      }

      socket.on('disconnect', () => {
        if (ip && clients.hasOwnProperty(ip)) {
          delete clients[ip];
        }
      });
    });

    let tick = () => {
      let _online = Object.keys(clients).length;
      socket_io.sockets.emit('changeOnlineEvent', { 'online': _online });
      setTimeout(tick, 2000);
    };
    setTimeout(tick, 2000);

and emits statistic to clients every 2 seconds. 并且每2秒向客户发送统计数据。 Looks easy. 看起来很容易

This is the client: 这是客户:

let socket = io.connect('http://domain:port',
{
  'reconnection': true,
  'reconnectionAttempts': 2
});
socket.on('changeOnlineEvent', (object) => {
 console.log(object.online);
});

But after few hours (6-12 hrs) of working every client starts making endless requests to the io.server: 但经过几个小时(6-12小时)的工作, 每个客户开始向io.server发出无休止的请求: 请求截图 As you can see every response is 200 OK and have right response body. 正如您所看到的,每个响应都是200 OK并且具有正确的响应体。

At this time I begin to see a terrible image in zabbix: 这时我开始在zabbix中看到一个可怕的图像: ZABBIX

Reload the page in browser does not help. 在浏览器中重新加载页面没有帮助。 And there is no one error in server's console while running io server app. 运行io服务器应用程序时,服务器控制台中没有任何错误。 Everything is ok. 一切都好。

The only thing that helps in this situation - restarting nodejs app :-( 在这种情况下唯一有用的东西 - 重启nodejs app :-(

I have no idea whats wrong and how to debug this. 我不知道什么是错的以及如何调试它。 Need help. 需要帮忙。 Thx. 谢谢。

  1. You can try to use only websocket transport. 您可以尝试仅使用websocket传输。 See https://socket.io/docs/client-api/#With-websocket-transport-only (the endless requests, because in polling mode client asking server for updates at certain regular intervals) 请参阅https://socket.io/docs/client-api/#With-websocket-transport-only (无限请求,因为在轮询模式下客户端要求服务器定期更新)
  2. You can get more logs from the server if you run the server as follows: DEBUG=engine:socket node index.js (socket.io uses debug package https://www.npmjs.com/package/debug ) 如果运行服务器,可以从服务器获取更多日志,如下所示: DEBUG=engine:socket node index.js (socket.io使用debughttps://www.npmjs.com/package/debug

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM