在nodejs中使用socket.io进行ping测试的延迟很大

时间:2016-02-04 02:08:51

标签: node.js websocket socket.io

运行这个简单的ping测试时,我遇到了25秒的一致延迟。服务器没有记录ping事件,但我假设它正在接收它,因为客户端正在收到回复。关于如何解决这个问题的任何想法?

客户端

    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();
    });

客户端日志

    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

服务器

    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 listening at port 3000
    client connected

1 个答案:

答案 0 :(得分:2)

我的答案是因为它解决了你的问题:

Socket.io正在使用pingpong作为实现内部的消息名称。更改为不同的消息名称。