Skip to content

Instantly share code, notes, and snippets.

@yoursunny
Created October 4, 2012 18:21
Show Gist options
  • Save yoursunny/3835425 to your computer and use it in GitHub Desktop.
Save yoursunny/3835425 to your computer and use it in GitHub Desktop.
CCNx WebSocket proxy
var WebSocketServer = require('ws').Server;
var net = require('net');
var wss = new WebSocketServer({port:9696,host:'0.0.0.0'});
wss.on('connection', function(ws) {
console.log('wsconn');
var sock_ready = false;
var send_queue = [];
var sock = net.createConnection(9695);
ws.on('message',function(message){
console.log(message);
if (sock_ready) {
sock.write(message);
} else {
send_queue.push(message);
}
});
sock.on('connect',function(){
while (send_queue.length > 0) {
var message = send_queue.shift();
sock.write(message);
}
sock_ready = true;
console.log('sockready');
});
sock.on('data',function(data){
ws.send(data);
});
sock.on('end',function(){
ws.terminate();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment