Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / http-proxy / examples / balancer / simple-balancer-with-websockets.js
1 var http = require('http'),
2     httpProxy = require('../../lib/node-http-proxy');
3
4 //
5 // A simple round-robin load balancing strategy.
6 // 
7 // First, list the servers you want to use in your rotation.
8 //
9 var addresses = [
10   {
11     host: 'ws1.0.0.0',
12     port: 80
13   },
14   {
15     host: 'ws2.0.0.0',
16     port: 80
17   }
18 ];
19
20 //
21 // Create a HttpProxy object for each target
22 //
23
24 var proxies = addresses.map(function (target) {
25   return new httpProxy.HttpProxy({
26     target: target
27   });
28 });
29
30 //
31 // Get the proxy at the front of the array, put it at the end and return it
32 // If you want a fancier balancer, put your code here
33 //
34
35 function nextProxy() {
36   var proxy = proxies.shift();
37   proxies.push(proxy);
38   return proxy;
39 }
40
41 // 
42 // Get the 'next' proxy and send the http request 
43 //
44
45 var server = http.createServer(function (req, res) {    
46   nextProxy().proxyRequest(req, res);
47 });
48
49 // 
50 // Get the 'next' proxy and send the upgrade request 
51 //
52
53 server.on('upgrade', function (req, socket, head) {
54   nextProxy().proxyWebSocketRequest(req, socket, head);
55 });
56
57 server.listen(8080);  
58