本文介绍了在 nodejs 中使用 socket.io 进行 ping 测试的巨大延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

客户

    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

推荐答案

把我的评论变成答案,因为它解决了你的问题:

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

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

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

这篇关于在 nodejs 中使用 socket.io 进行 ping 测试的巨大延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 02:43