Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
this.app.on('upgrade', (request, socket, body) => {
if (!WebSocket.isWebSocket(request)) return;
var ws = new WebSocket(request, socket, body);
ws.on('message', event => {
var data = event.data, json;
try {
json = JSON.parse(event.data);
if (typeof json.command === 'string') {
this.handleCommand(json);
}
} catch(e) { }
});
ws.on('close', event => {
this.sockets = this.sockets.filter( sock => (sock !== ws) );
ws = null;
});
constructor (req, socket, head, options = {}) {
super();
this.options = options;
this.ws = new WebSocket(req, socket, head);
this.ws.onmessage = this.message.bind(this);
this.ws.onclose = this.close.bind(this);
this.id = this.uniqueId('ws');
}