Last active
February 23, 2022 17:09
-
-
Save sahat/8364120 to your computer and use it in GitHub Desktop.
Calculate client-server latency using socket.io
This file contains 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('ping'); | |
}, 2000); | |
socket.on('pong', function() { | |
latency = Date.now() - startTime; | |
console.log(latency); | |
}); |
This file contains 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('ping', function() { | |
socket.emit('pong'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://stackoverflow.com/a/45216554/1221082