Created
April 16, 2013 17:32
-
-
Save belak/5397861 to your computer and use it in GitHub Desktop.
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
| // Any colors for output | |
| var colors = require('colors'); | |
| // For json sockets | |
| var jot = require('json-over-tcp'); | |
| // Setup socket.io | |
| var app = require('http').createServer(); | |
| var io = require('socket.io').listen(app); | |
| var rl = require('readline').createInterface({ | |
| input: process.stdin, | |
| output: process.stdout, | |
| // Note: This is a workaround because closing stdin doesn't work if this is true | |
| terminal: false | |
| }); | |
| // Any commands we need to respond to | |
| var cmds = []; | |
| // Go port, listen port (defaults) | |
| var ports = [53211, 11235]; | |
| // Loop through the args and store any ports passed in through the command line | |
| process.argv.forEach(function (val, index, array) { | |
| // Ignore anything we don't want | |
| if (index < 2 || index >= 4) { | |
| return; | |
| } | |
| ports[index-2] = val; | |
| }); | |
| // Tell the server to listen on a port | |
| app.listen(ports[1]); | |
| rl.on('line', function(text) { | |
| console.log('adding cmd: '.green + text); | |
| cmds.push(text); | |
| }); | |
| // When we're done reading commands | |
| rl.on('close', function(text) { | |
| // On each connection | |
| io.sockets.on('connection', function (socket) { | |
| // Open a new socket to the Go server | |
| var go_socket = jot.connect(ports[0], function() { | |
| // Send back any data we get | |
| go_socket.on('data', function(data) { | |
| socket.emit(data.command, data.message) | |
| }); | |
| // Start proxying commands | |
| for (var i in cmds) { | |
| // For any allowed function | |
| socket.on(cmds[i], function (data) { | |
| var send_data = { | |
| command: cmds[i], | |
| message: data | |
| }; | |
| go_socket.write(send_data); | |
| }); | |
| } | |
| }); | |
| }); | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment