|
| 1 | +/* |
| 2 | + demo.js: http proxy for node.js |
| 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 | + websocket = require('./websocket'), |
| 30 | + utils = require('socket.io/lib/socket.io/utils'), |
| 31 | + io = require('socket.io'), |
| 32 | + httpProxy = require('./../lib/node-http-proxy'); |
| 33 | + |
| 34 | +// |
| 35 | +// Create the target HTTP server |
| 36 | +// |
| 37 | +var server = http.createServer(function (req, res) { |
| 38 | + res.writeHead(200); |
| 39 | + res.end(); |
| 40 | +}); |
| 41 | +server.listen(8080); |
| 42 | + |
| 43 | +// |
| 44 | +// Setup socket.io on the target HTTP server |
| 45 | +// |
| 46 | +var socket = io.listen(server); |
| 47 | +socket.on('connection', function (client) { |
| 48 | + sys.debug('Got websocket connection'); |
| 49 | + |
| 50 | + client.on('message', function (msg) { |
| 51 | + sys.debug('Got message from client: ' + msg); |
| 52 | + }); |
| 53 | + |
| 54 | + socket.broadcast('from server'); |
| 55 | +}); |
| 56 | + |
| 57 | +// |
| 58 | +// Create a proxy server with node-http-proxy |
| 59 | +// |
| 60 | +var proxy = httpProxy.createServer(8080, 'localhost'); |
| 61 | +proxy.listen(8081); |
| 62 | + |
| 63 | +// |
| 64 | +// Setup the web socket against our proxy |
| 65 | +// |
| 66 | +var ws = new websocket.WebSocket('ws://localhost:8081/socket.io/websocket/', 'borf'); |
| 67 | + |
| 68 | +ws.on('open', function () { |
| 69 | + ws.send(utils.encode('from client')); |
| 70 | +}); |
| 71 | + |
| 72 | +ws.on('message', function (msg) { |
| 73 | + sys.debug('Got message: ' + utils.decode(msg)); |
| 74 | +}); |
0 commit comments