-
-
Save AlphaT7/b91f9086ccbd6aab9928c4d4adbd96c9 to your computer and use it in GitHub Desktop.
Calculate client-server latency using socket.io
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var socket = io.connect('http://localhost'); | |
| var startTime; | |
| setInterval(function() { | |
| startTime = Date.now(); | |
| socket.emit('client2server'); | |
| }, 2000); | |
| socket.on('server2client', function() { | |
| latency = Date.now() - startTime; | |
| console.log(latency); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| io.sockets.on('connection', function (socket) { | |
| socket.on('client2server', function() { | |
| socket.emit('pong'); | |
| }); | |
| }); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Changed the default: 'ping' and 'pong" event names to: 'client2server' and 'server2client'. Apparently 'ping' and 'pong' are already named events in socket io, and only trigger every 25 seconds.