Skip to content

Instantly share code, notes, and snippets.

@AlphaT7
Forked from sahat/client.js
Last active August 11, 2017 21:22
Show Gist options
  • Select an option

  • Save AlphaT7/b91f9086ccbd6aab9928c4d4adbd96c9 to your computer and use it in GitHub Desktop.

Select an option

Save AlphaT7/b91f9086ccbd6aab9928c4d4adbd96c9 to your computer and use it in GitHub Desktop.
Calculate client-server latency using socket.io
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);
});
io.sockets.on('connection', function (socket) {
socket.on('client2server', function() {
socket.emit('pong');
});
});
@AlphaT7

AlphaT7 commented Aug 11, 2017

Copy link
Copy Markdown
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment