Added simple round robin example with websocket support

This commit is contained in:
Oscar Östlund 2012-11-25 22:21:41 -05:00 committed by indexzero
parent 9672b99271
commit 83fbd42506

View File

@ -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);