Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / http-proxy / examples / websocket / latent-websocket-proxy.js
1 /*
2   standalone-websocket-proxy.js: Example of proxying websockets over HTTP with a standalone HTTP server.
3
4   Copyright (c) 2010 Charlie Robbins, Mikeal Rogers, Fedor Indutny, & Marak Squires.
5
6   Permission is hereby granted, free of charge, to any person obtaining
7   a copy of this software and associated documentation files (the
8   "Software"), to deal in the Software without restriction, including
9   without limitation the rights to use, copy, modify, merge, publish,
10   distribute, sublicense, and/or sell copies of the Software, and to
11   permit persons to whom the Software is furnished to do so, subject to
12   the following conditions:
13
14   The above copyright notice and this permission notice shall be
15   included in all copies or substantial portions of the Software.
16
17   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21   LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22   OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23   WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25 */
26
27 var util = require('util'),
28     http = require('http'),
29     colors = require('colors'),
30     httpProxy = require('../../lib/node-http-proxy');
31
32 try {
33   var io = require('socket.io'),
34       client = require('socket.io-client');
35 }
36 catch (ex) {
37   console.error('Socket.io is required for this example:');
38   console.error('npm ' + 'install'.green);
39   process.exit(1);
40 }
41
42 //
43 // Create the target HTTP server and setup
44 // socket.io on it.
45 //
46 var server = io.listen(8080);
47 server.sockets.on('connection', function (client) {
48   util.debug('Got websocket connection');
49
50   client.on('message', function (msg) {
51     util.debug('Got message from client: ' + msg);
52   });
53
54   client.send('from server');
55 });
56
57 //
58 // Setup our server to proxy standard HTTP requests
59 //
60 var proxy = new httpProxy.HttpProxy({
61   target: {
62     host: 'localhost', 
63     port: 8080
64   }
65 });
66
67 var proxyServer = http.createServer(function (req, res) {
68   proxy.proxyRequest(req, res);
69 });
70
71 //
72 // Listen to the `upgrade` event and proxy the 
73 // WebSocket requests as well.
74 //
75 proxyServer.on('upgrade', function (req, socket, head) {
76   var buffer = httpProxy.buffer(socket);
77   
78   setTimeout(function () {
79     proxy.proxyWebSocketRequest(req, socket, head, buffer);
80   }, 1000);
81 });
82
83 proxyServer.listen(8081);
84
85 //
86 // Setup the socket.io client against our proxy
87 //
88 var ws = client.connect('ws://localhost:8081');
89
90 ws.on('message', function (msg) {
91   util.debug('Got message: ' + msg);
92 });