From 83fbd4250660f41de1ab2b5490a3bf58200ae148 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oscar=20O=CC=88stlund?= Date: Sun, 25 Nov 2012 22:21:41 -0500 Subject: [PATCH] Added simple round robin example with websocket support --- .../simple-balancer-with-websockets.js | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 examples/balancer/simple-balancer-with-websockets.js diff --git a/examples/balancer/simple-balancer-with-websockets.js b/examples/balancer/simple-balancer-with-websockets.js new file mode 100644 index 0000000..efa0c1c --- /dev/null +++ b/examples/balancer/simple-balancer-with-websockets.js @@ -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); + \ No newline at end of file