Skip to content

Instantly share code, notes, and snippets.

@Kukunin
Created December 9, 2017 19:49
Show Gist options
  • Save Kukunin/101f5e5570b0a6dbead1f5054f212ba2 to your computer and use it in GitHub Desktop.
Save Kukunin/101f5e5570b0a6dbead1f5054f212ba2 to your computer and use it in GitHub Desktop.
Simulate a TCP socket with `ws` in NOMP based pools
var events = require('events');
var ws = require('ws');
function simulateTcpSocketFromWS(ws) {
var socket = {};
socket.remoteAddress = ws._socket.remoteAddress;
socket.localPort = ws._socket.localPort;
socket.setKeepAlive = ws._socket.setKeepAlive;
socket.writable = true;
socket.setEncoding = function() {};
socket.destroy = function() {
ws.removeAllListeners('close');
ws.close();
};
socket.write = function(data) {
ws.send(data, function(error) {
if (error) {
socket.emit('error', error);
}
});
};
ws.on('message', function(data) {
socket.emit('data', data);
});
ws.on('close', function() {
socket.emit('close');
});
ws.on('error', function(e) {
socket.emit('error', e);
});
socket.__proto__ = events.EventEmitter.prototype;
return socket;
};
wss = new ws.Server();
wss.on('connection', function(ws) {
newClientCallback(simulateTcpSocketFromWS(ws));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment