From 56003b527625b2d83a191f3172005c87856aa87d Mon Sep 17 00:00:00 2001 From: Fedor Indutny Date: Thu, 16 Sep 2010 11:43:35 +0700 Subject: [PATCH] Added support of automatic websocket tunneling, added test for it --- lib/node-http-proxy.js | 20 ++++++++++++++------ test/node-http-proxy-websocket-test.js | 5 ++++- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/lib/node-http-proxy.js b/lib/node-http-proxy.js index 15eec2961..8c4c2a67a 100644 --- a/lib/node-http-proxy.js +++ b/lib/node-http-proxy.js @@ -60,9 +60,17 @@ exports.createServer = function () { } }); - // WebSocket support - server.on('update', function() { - }); + // If callback is empty - tunnel websocket request automatically + if (!callback) { + // WebSocket support + server.on('upgrade', function(req, socket, head) { + var proxy = new HttpProxy(req, socket, head); + + // Tunnel websocket requests too + proxy.proxyWebSocketRequest(port, host); + + }); + } return server; }; @@ -325,12 +333,12 @@ HttpProxy.prototype = { request.end(); socket.end(); } - + // Catch socket errors socket.on('error', function() { request.end(); }); - + // Remove data listener request.socket.removeListener('data', t); }); @@ -344,7 +352,7 @@ HttpProxy.prototype = { } self.unwatch(socket); }); - + // Request function onUpgrade(reverse_proxy) { diff --git a/test/node-http-proxy-websocket-test.js b/test/node-http-proxy-websocket-test.js index c56370e56..25e373e83 100644 --- a/test/node-http-proxy-websocket-test.js +++ b/test/node-http-proxy-websocket-test.js @@ -48,4 +48,7 @@ server.on('upgrade', function(req, socket, head) { p.proxyWebSocketRequest(8080, 'localhost'); }); -server.listen(80); +server.listen(8000); + +httpProxy.createServer(8080, 'localhost').listen(8001); +