Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@photofroggy
Created September 4, 2012 01:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save photofroggy/3615427 to your computer and use it in GitHub Desktop.
Save photofroggy/3615427 to your computer and use it in GitHub Desktop.
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