Skip to content

Commit 1ee8ae7

Browse files
committed
[doc] Add examples/standalone-websocket-proxy.js
1 parent b608a02 commit 1ee8ae7

File tree

3 files changed

+104
-2
lines changed

3 files changed

+104
-2
lines changed

examples/standalone-proxy.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
standalone-proxy.js: Example of proxying over HTTP inside of a standalone HTTP server.
2+
standalone-proxy.js: Example of proxying over HTTP with a standalone HTTP server.
33
44
Copyright (c) 2010 Charlie Robbins, Mikeal Rogers, Fedor Indutny, & Marak Squires.
55
+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
proxy.proxyWebSocketRequest(req, socket, head, {
84+
port: 8080,
85+
host: 'localhost'
86+
});
87+
});
88+
89+
proxyServer.listen(8081);
90+
91+
//
92+
// Setup the web socket against our proxy
93+
//
94+
var ws = new websocket.WebSocket('ws://localhost:8081/socket.io/websocket/', 'borf');
95+
96+
ws.on('open', function () {
97+
ws.send(utils.encode('from client'));
98+
});
99+
100+
ws.on('message', function (msg) {
101+
sys.debug('Got message: ' + utils.decode(msg));
102+
});

examples/web-socket-proxy.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ try {
3636
}
3737
catch (ex) {
3838
console.error('Socket.io is required for this example:');
39-
console.error('npm ' + 'install'.green + ' socket.io'.magenta);
39+
console.error('npm ' + 'install'.green + ' socket.io@0.6.18'.magenta);
4040
process.exit(1);
4141
}
4242

0 commit comments

Comments
 (0)