Skip to content

Instantly share code, notes, and snippets.

@whiteinge
Created September 16, 2017 00:57
Show Gist options
  • Save whiteinge/449d24c927af9b08432b23517a7a8d4e to your computer and use it in GitHub Desktop.
Save whiteinge/449d24c927af9b08432b23517a7a8d4e to your computer and use it in GitHub Desktop.
Verbose node.js websocket logging server
const express = require('express');
const http = require('http');
const url = require('url');
const WebSocket = require('ws');
const app = express();
app.use(function (req, res) {
res.send({});
});
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });
function heartbeat() {
this.isAlive = true;
}
wss.on('connection', function connection(ws, req) {
const location = url.parse(req.url, true);
// You might use location.query.access_token to authenticate or share sessions
// or req.headers.cookie (see http://stackoverflow.com/a/16395220/151312)
ws.on('open', function open() {
console.log('connected');
});
ws.on('close', function close() {
console.log('disconnected');
});
ws.on('message', function incoming(message) {
console.log('received: %s', message);
});
ws.isAlive = true;
ws.on('pong', heartbeat);
const interval = setInterval(function ping() {
wss.clients.forEach(function each(ws) {
if (ws.isAlive === false) {
console.log('Stale connection terminating.');
return ws.terminate();
}
ws.isAlive = false;
ws.ping('', false, true);
});
}, 10000);
console.log('First connected');
ws.send('{}');
});
server.listen(8080, function listening() {
console.log('Listening on %d', server.address().port);
});
@whiteinge
Copy link
Author

whiteinge commented Sep 19, 2017

Req's npm install ws express. Most of this is just copy-and-pasted from the ws README.

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