Last active
March 28, 2019 13:31
-
-
Save servercharlie/f3c85dbbfa8308e83257dc2ee31cd7cc to your computer and use it in GitHub Desktop.
Proper Socket.IO Latency Checking
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
/* | |
- Insert this after including / referencing socket.io.min.js. | |
- Point being: | |
- Client sockets *naturally* adapts its Ping Interval & Ping Timeout | |
from its server's settings, hence you can't actually those options here. | |
- We instead add an eventhandler for the "pong", and the actual millisecond latency, | |
and optionally to the "ping" too, in case you wanna see it in action. | |
- References: | |
- Server API @ https://github.com/socketio/socket.io/blob/master/docs/API.md | |
- Client API @ https://github.com/socketio/socket.io-client/blob/master/docs/API.md | |
*/ | |
if(io){ | |
/* | |
Alternative: | |
var HTTP_SERVER = "127.0.0.1"; // or "YourAwesomeDomainName.com" | |
var HTTP_PORT = 3000; | |
var socket = io(`http://${HTTP_SERVER}:${HTTP_PORT}/`); | |
*/ | |
var CLIENT_SOCKET = io(`http://${window.location.host}/`); | |
CLIENT_SOCKET.on('connect', function(){ | |
console.log(`Socket :: Connected.`); | |
}); | |
CLIENT_SOCKET.on('disconnect', function(){ | |
console.log(`Socket :: Disconnected.`); | |
}); | |
CLIENT_SOCKET.on('ping', function(){ | |
console.log(`Socket :: Ping sent.`); | |
}); | |
CLIENT_SOCKET.on('pong', function(ms){ | |
console.log(`Socket :: Latency :: ${ms} ms`); | |
}); | |
}else{ | |
console.log("Error :: Socket IO not found!"); | |
} |
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
/* | |
- Run w/ NodeJS | |
- Needs: | |
- Express (http://expressjs.com/ | |
- npm install express --save | |
- Socket.IO | |
- npm install socket.io --save | |
- Point being: | |
- We set "pingInterval" and "pingTimeout" in the options variable, | |
which is available upon creating a socket.io server instance. | |
- Here we set the pingInterval to 2000, and the pingTimeout to 5000 | |
- References: | |
- Server API @ https://github.com/socketio/socket.io/blob/master/docs/API.md | |
- Client API @ https://github.com/socketio/socket.io-client/blob/master/docs/API.md | |
*/ | |
var path = require('path'); | |
var EXPRESS = require('express'); | |
var HTTP = require('http'); | |
var EXPRESS_INSTANCE = EXPRESS(); | |
var HTTP_PORT = 3000; | |
var HTTP_SERVER = HTTP.Server(EXPRESS_INSTANCE); | |
/* | |
This exposes the /static/ folder, since our files are hosted there. | |
/sample-project-folder/ | |
- server.js | |
- static | |
- index.html | |
- client.js | |
- otherfileswhatever.js | |
Change it as you like. | |
*/ | |
EXPRESS_INSTANCE.use(EXPRESS.static(path.join(__dirname, './static/'))); | |
HTTP_SERVER.listen(HTTP_PORT, function(){ | |
console.log(`HTTP_SERVER Listening on ${HTTP_PORT}`); | |
}); | |
var SOCKET_IO_SERVER = require('socket.io')( | |
HTTP_SERVER, | |
{ | |
'pingInterval': 2000, | |
'pingTimeout': 5000 | |
}); | |
SOCKET_IO_SERVER.on('connection', function(CLIENT_SOCKET){ | |
console.log('A user connected'); | |
CLIENT_SOCKET.on('disconnect', function(){ | |
console.log('user disconnected'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
kl