-
-
Save gabrielstuff/5241d87eb888abb4f17fa9249af0f182 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 = 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); |
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 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