Skip to content

Commit 83fbd42

Browse files
oostindexzero
authored andcommitted
Added simple round robin example with websocket support
1 parent 9672b99 commit 83fbd42

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
var httpProxy = require('../../lib/node-http-proxy');
2+
//
3+
// A simple round-robin load balancing strategy.
4+
//
5+
// First, list the servers you want to use in your rotation.
6+
//
7+
var addresses = [
8+
{
9+
host: 'ws1.0.0.0',
10+
port: 80
11+
},
12+
{
13+
host: 'ws2.0.0.0',
14+
port: 80
15+
}
16+
];
17+
18+
var proxies = addresses.map(function (target) {
19+
return new httpProxy.HttpProxy({
20+
target: target
21+
});
22+
});
23+
24+
function nextProxy() {
25+
var proxy = proxies.shift();
26+
proxies.push(proxy);
27+
return proxy;
28+
}
29+
30+
var server = http.createServer(function (req, res) {
31+
nextProxy().proxyRequest(req, res);
32+
});
33+
34+
server.on('upgrade', function(req, socket, head) {
35+
nextProxy().proxyWebSocketRequest(req, socket, head);
36+
});
37+
38+
server.listen(8080);
39+

0 commit comments

Comments
 (0)