Created
February 18, 2014 00:59
-
-
Save AdamMagaluk/9062488 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
var http = require('http'); | |
var net = require('net'); | |
var async = require('async'); | |
var websocket = require('websocket-stream'); | |
var Parser = require('./parser.js'); | |
var multiplexer = new Parser(); | |
function proxyws(id,callback){ | |
console.log('proxy start ' + id); | |
var ws = websocket('ws://localhost:3000/') | |
ws.on('open',function(){ | |
console.log('ws open',id); | |
var client = net.connect({port: 1337}); | |
ws.pipe(client).pipe(ws); | |
}) | |
ws.on('error', function(err) { | |
console.log('ws error',id); | |
callback(err); | |
}); | |
ws.on('end', function(err) { | |
console.log('ws end',id); | |
callback(); | |
}); | |
} | |
async.forever(proxyws.bind(null,0),function(err){}) | |
async.forever(proxyws.bind(null,1),function(err){}) | |
async.forever(proxyws.bind(null,2),function(err){}) | |
async.forever(proxyws.bind(null,3),function(err){}) | |
async.forever(proxyws.bind(null,4),function(err){}) | |
http.createServer(function (req, res) { | |
res.writeHead(200, {'Content-Type': 'text/plain'}); | |
res.end('Hello World\n'); | |
}).listen(1337, '127.0.0.1'); |
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
var WebSocketServer = require('ws').Server | |
var websocket = require('websocket-stream') | |
var wss = new WebSocketServer({port : 3000}) | |
var allStreams = []; | |
wss.on('connection', function(ws) { | |
var stream = websocket(ws) | |
allStreams.push(stream); | |
stream.on('error',function(err){ | |
allStreams.forEach(function(s,idx){ | |
if(s === stream){ | |
allStreams.splice(idx,1); | |
console.log('found stream',idx); | |
} | |
}) | |
}); | |
stream.on('end',function(){ | |
allStreams.forEach(function(s,idx){ | |
if(s === stream){ | |
allStreams.splice(idx,1); | |
console.log('found stream',idx); | |
} | |
}) | |
}) | |
}) | |
var net = require('net'); | |
var server = net.createServer(function(c) { //'connection' listener | |
c.on('end', function() { | |
console.log('server disconnected'); | |
}); | |
var stream = allStreams[0]; | |
c.pipe(stream).pipe(c); | |
//c.pipe(steam).pipe(c); | |
}); | |
server.listen(3001, function() { //'listening' listener | |
console.log('server bound'); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment