Huge delay from ping test with socket.io in nodejs

2019-06-06 18:02发布

I am experiencing consistent delay of 25 seconds when running this simple ping test. The server is not logging the ping event but I am assuming it is receiving it because the client is getting a reply. Any ideas on how to fix this?

client

    var io = require('socket.io-client')('http://68.12.157.176:3000')
    var ping_time = Date.now();

    function ping(){
      ping_time = Date.now();
      console.log('sending ping...');
      io.emit('ping');
    }
    ping();

    io.on('pong', function (data) {
      ping_time = Date.now() - ping_time;
      console.log("replied in " + ping_time + "ms");
      ping();
    });

client log

    sending ping...
    replied in 25202ms
    sending ping...
    replied in 25028ms
    sending ping...
    replied in 25029ms
    sending ping...
    replied in 25032ms
    sending ping...
    replied in 25016ms

server

    var express = require('express');
    var app = express();
    var server = require('http').createServer(app);
    var io = require('../..')(server);
    var port = process.env.PORT || 3000;

    server.listen(port, function () {
      console.log('Server listening at port %d', port);
    });

    io.on('connection', function (socket) {
      console.log('client connected');
      socket.on('ping', function (data) {
        console.log("received ping, sending reply"); //no log shown???
        socket.emit('pong'); //client receives event ~25 seconds after request
      });
    });

server log

    Server listening at port 3000
    client connected

1条回答
老娘就宠你
2楼-- · 2019-06-06 18:40

Making my comment into an answer since it solved your problem:

Socket.io is using ping and pong as message names itself internal to the implementation. Change to different message names.

查看更多
登录 后发表回答