Created
December 9, 2017 19:49
-
-
Save Kukunin/101f5e5570b0a6dbead1f5054f212ba2 to your computer and use it in GitHub Desktop.
Simulate a TCP socket with `ws` in NOMP based pools
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
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