How can I find the response time (latency) of a client in NodeJS with sockets (socket.io)?

后端 未结 6 979
故里飘歌
故里飘歌 2021-01-30 07:11

I\'m trying to create a multiplayer game with NodeJS and I want to synchronize the action between clients.

What would be the best way to find the latency (the time that

6条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-30 07:49

    Heres my really quick and dirty script to test the ping ... just head to http://yourserver:8080 in your browser and watch the console (ssh terminal for me).

    var http = require('http');
    var io = require('socket.io');
    
    server = http.createServer(function (req, res) {
      res.writeHead(200, {'Content-Type': 'text/html'});
      res.write('\n');
      res.write('  \n');
      res.write('    Node Ping\n');
      res.write('    \n');
      res.write('    \n');
      res.write('  \n');
      res.write('  \n');
      res.write('    

    Node Ping

    \n'); res.write(' \n'); res.write('\n'); res.end(); }); server.listen(8080); console.log('Server running at http://127.0.0.1:8080/'); var socket = io.listen(server); socket.on('connection',function(client){ var start = new Date().getTime(); client.send(1); client.on('message',function(message){ client.send(1); console.log( new Date$ client.on('disconnect',function(){}); });

    I'm very curious about this because it seems like my pings are pretty high(200-400ms round trip) on large vps boxes w/ dedicated resources both in california and new jersey. (I'm on the east coast) I'm betting theres just a lot of latency on the vps boxes b/c they're serving so much traffic?

    The thing that gets me is that a regular ping from the linux terminal from the same client to the same server is 11ms on average a factor of 10 lower ... am I doing something wrong or is something slow with node.js/socket.io/websockets?

提交回复
热议问题