Skip to content

Commit ed8c9ee

Browse files
committed
[examples] updated websockets examples
1 parent 588327c commit ed8c9ee

File tree

3 files changed

+249
-0
lines changed

3 files changed

+249
-0
lines changed

Diff for: examples/websocket/latent-websocket-proxy.js

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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/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(9000);
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.createProxyServer({
61+
target: {
62+
host: 'localhost',
63+
port: 9000
64+
}
65+
});
66+
67+
var proxyServer = http.createServer(function (req, res) {
68+
proxy.web(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+
setTimeout(function () {
77+
proxy.ws(req, socket, head);
78+
}, 1000);
79+
});
80+
81+
proxyServer.listen(8000);
82+
83+
//
84+
// Setup the socket.io client against our proxy
85+
//
86+
var ws = client.connect('ws://localhost:8000');
87+
88+
ws.on('message', function (msg) {
89+
util.debug('Got message: ' + msg);
90+
ws.send('I am the client');
91+
});

Diff for: examples/websocket/standalone-websocket-proxy.js

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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/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(9000);
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.createProxyServer({
61+
target: {
62+
host: 'localhost',
63+
port: 9000
64+
}
65+
});
66+
var proxyServer = http.createServer(function (req, res) {
67+
proxy.web(req, res);
68+
});
69+
70+
//
71+
// Listen to the `upgrade` event and proxy the
72+
// WebSocket requests as well.
73+
//
74+
proxyServer.on('upgrade', function (req, socket, head) {
75+
proxy.ws(req, socket, head);
76+
});
77+
78+
proxyServer.listen(8000);
79+
80+
//
81+
// Setup the socket.io client against our proxy
82+
//
83+
var ws = client.connect('ws://localhost:8000');
84+
85+
ws.on('message', function (msg) {
86+
util.debug('Got message: ' + msg);
87+
ws.send('I am the client');
88+
});

Diff for: examples/websocket/websocket-proxy.js

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
web-socket-proxy.js: Example of proxying over HTTP and WebSockets.
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/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(9000);
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+
// Create a proxy server with node-http-proxy
59+
//
60+
httpProxy.createServer({ target: 'ws://localhost:9000', ws: true }).listen(8000);
61+
62+
//
63+
// Setup the socket.io client against our proxy
64+
//
65+
var ws = client.connect('ws://localhost:8000');
66+
67+
ws.on('message', function (msg) {
68+
util.debug('Got message: ' + msg);
69+
ws.send('I am the client');
70+
});

0 commit comments

Comments
 (0)