Skip to content

Instantly share code, notes, and snippets.

@gabrielstuff
Forked from sahat/client.js
Last active September 2, 2016 21:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabrielstuff/5241d87eb888abb4f17fa9249af0f182 to your computer and use it in GitHub Desktop.
Save gabrielstuff/5241d87eb888abb4f17fa9249af0f182 to your computer and use it in GitHub Desktop.
Calculate client-server latency using socket.io
var socket = require('socket.io-client')('http://spacebro.space:8080');
var startTime;
setInterval(function() {
socket.emit('latency', Date.now(), function(startTime) {
var latency = Date.now() - startTime;
console.log(latency);
})
}, 2000);
var io = require('socket.io')(80);
io.on('connection', function (socket) {
socket.on('latency', function (startTime, cb) {
cb(startTime)
})
socket.on('disconnect', function () {
io.emit('user disconnected')
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment