mirror of
https://github.com/http-party/node-http-proxy.git
synced 2025-12-08 20:59:18 +00:00
Added simple round robin example with websocket support
This commit is contained in:
parent
9672b99271
commit
83fbd42506
39
examples/balancer/simple-balancer-with-websockets.js
Normal file
39
examples/balancer/simple-balancer-with-websockets.js
Normal 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);
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user