Created
October 4, 2012 18:21
-
-
Save yoursunny/3835425 to your computer and use it in GitHub Desktop.
CCNx WebSocket proxy
This file contains 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 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