Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function proxy( app ) {
var serve = http.createServer();
serve.listen(3900);
wsserve = new wss({
httpServer: serve,
autoAcceptConnections: false
});
function allowOrigin( req ) {
return req.host.split(':')[0] == req.origin.split('://')[1];
}
wsserve.on('request', function( req ) {
if( !allowOrigin( req ) ) {
req.reject();
return;
}
var connection = req.accept(null, req.origin);
var client = net.connect({host: 'addr', port: port});
var buf = '';
client.on('data', function(data) {
buf+= data.toString();
parts = buf.split('\0');
while( parts.length > 1 ) {
pkt = parts.shift();
if( pkt.length > 0 ) {
connection.sendUTF( pkt );
}
}
if( parts[0].length == 0 ) {
buf = '';
} else {
buf = parts[0];
}
});
client.on('end', function() {
connection.close();
});
connection.on('message', function (message) {
client.write(message.utf8Data + '\0');
});
connection.on('close', function ( error, description ) {
client.end();
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.