Skip to content

Commit fcfe846

Browse files
committed
[doc] Added examples/latent-websocket-proxy.js
1 parent 1ee8ae7 commit fcfe846

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

examples/latent-websocket-proxy.js

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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 sys = require('sys'),
28+
http = require('http'),
29+
colors = require('colors'),
30+
websocket = require('./../vendor/websocket'),
31+
httpProxy = require('./../lib/node-http-proxy');
32+
33+
try {
34+
var utils = require('socket.io/lib/socket.io/utils'),
35+
io = require('socket.io');
36+
}
37+
catch (ex) {
38+
console.error('Socket.io is required for this example:');
39+
console.error('npm ' + 'install'.green + ' [email protected]'.magenta);
40+
process.exit(1);
41+
}
42+
43+
//
44+
// Create the target HTTP server
45+
//
46+
var server = http.createServer(function (req, res) {
47+
res.writeHead(200);
48+
res.end();
49+
});
50+
51+
server.listen(8080);
52+
53+
//
54+
// Setup socket.io on the target HTTP server
55+
//
56+
var socket = io.listen(server);
57+
socket.on('connection', function (client) {
58+
sys.debug('Got websocket connection');
59+
60+
client.on('message', function (msg) {
61+
sys.debug('Got message from client: ' + msg);
62+
});
63+
64+
socket.broadcast('from server');
65+
});
66+
67+
//
68+
// Setup our server to proxy standard HTTP requests
69+
//
70+
var proxy = new httpProxy.HttpProxy();
71+
var proxyServer = http.createServer(function (req, res) {
72+
proxy.proxyRequest(req, res, {
73+
host: 'localhost',
74+
port: 8080
75+
})
76+
});
77+
78+
//
79+
// Listen to the `upgrade` event and proxy the
80+
// WebSocket requests as well.
81+
//
82+
proxyServer.on('upgrade', function (req, socket, head) {
83+
var buffer = proxy.buffer(socket);
84+
85+
setTimeout(function () {
86+
proxy.proxyWebSocketRequest(req, socket, head, {
87+
port: 8080,
88+
host: 'localhost',
89+
buffer: buffer
90+
});
91+
}, 1000);
92+
});
93+
94+
proxyServer.listen(8081);
95+
96+
//
97+
// Setup the web socket against our proxy
98+
//
99+
var ws = new websocket.WebSocket('ws://localhost:8081/socket.io/websocket/', 'borf');
100+
101+
ws.on('open', function () {
102+
ws.send(utils.encode('from client'));
103+
});
104+
105+
ws.on('message', function (msg) {
106+
sys.debug('Got message: ' + utils.decode(msg));
107+
});

0 commit comments

Comments
 (0)