Skip to content

Commit 56003b5

Browse files
committed
Added support of automatic websocket tunneling, added test for it
1 parent cd78af5 commit 56003b5

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

Diff for: lib/node-http-proxy.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,17 @@ exports.createServer = function () {
6060
}
6161
});
6262

63-
// WebSocket support
64-
server.on('update', function() {
65-
});
63+
// If callback is empty - tunnel websocket request automatically
64+
if (!callback) {
65+
// WebSocket support
66+
server.on('upgrade', function(req, socket, head) {
67+
var proxy = new HttpProxy(req, socket, head);
68+
69+
// Tunnel websocket requests too
70+
proxy.proxyWebSocketRequest(port, host);
71+
72+
});
73+
}
6674

6775
return server;
6876
};
@@ -325,12 +333,12 @@ HttpProxy.prototype = {
325333
request.end();
326334
socket.end();
327335
}
328-
336+
329337
// Catch socket errors
330338
socket.on('error', function() {
331339
request.end();
332340
});
333-
341+
334342
// Remove data listener
335343
request.socket.removeListener('data', t);
336344
});
@@ -344,7 +352,7 @@ HttpProxy.prototype = {
344352
}
345353
self.unwatch(socket);
346354
});
347-
355+
348356
// Request
349357

350358
function onUpgrade(reverse_proxy) {

Diff for: test/node-http-proxy-websocket-test.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,7 @@ server.on('upgrade', function(req, socket, head) {
4848
p.proxyWebSocketRequest(8080, 'localhost');
4949
});
5050

51-
server.listen(80);
51+
server.listen(8000);
52+
53+
httpProxy.createServer(8080, 'localhost').listen(8001);
54+

0 commit comments

Comments
 (0)