Skip to content

Instantly share code, notes, and snippets.

@servercharlie
Last active March 28, 2019 13:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save servercharlie/f3c85dbbfa8308e83257dc2ee31cd7cc to your computer and use it in GitHub Desktop.
Save servercharlie/f3c85dbbfa8308e83257dc2ee31cd7cc to your computer and use it in GitHub Desktop.
Proper Socket.IO Latency Checking
/*
- 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!");
}
/*
- 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');
});
});
@savone
Copy link

savone commented Mar 28, 2019

kl

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