Skip to content

Commit

Permalink
Added simple round robin example with websocket support
Browse files Browse the repository at this point in the history
  • Loading branch information
oost authored and indexzero committed Mar 9, 2013
1 parent 9672b99 commit 83fbd42
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions examples/balancer/simple-balancer-with-websockets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
var httpProxy = require('../../lib/node-http-proxy');
//
// A simple round-robin load balancing strategy.
//
// First, list the servers you want to use in your rotation.
//
var addresses = [
{
host: 'ws1.0.0.0',
port: 80
},
{
host: 'ws2.0.0.0',
port: 80
}
];

var proxies = addresses.map(function (target) {
return new httpProxy.HttpProxy({
target: target
});
});

function nextProxy() {
var proxy = proxies.shift();
proxies.push(proxy);
return proxy;
}

var server = http.createServer(function (req, res) {
nextProxy().proxyRequest(req, res);
});

server.on('upgrade', function(req, socket, head) {
nextProxy().proxyWebSocketRequest(req, socket, head);
});

server.listen(8080);

0 comments on commit 83fbd42

Please sign in to comment.