2 * ws.js: Top level include for node-http-proxy websocket helpers
4 * (C) 2010 Nodejitsu Inc.
9 var assert = require('assert'),
10 https = require('https'),
11 async = require('async'),
12 io = require('socket.io'),
14 helpers = require('./index'),
15 protocols = helpers.protocols,
16 http = require('./http');
19 // ### function createServerPair (options, callback)
20 // #### @options {Object} Options to create target and proxy server.
21 // #### @target {Object} Options for the target server.
22 // #### @proxy {Object} Options for the proxy server.
23 // #### @callback {function} Continuation to respond to when complete.
25 // Creates http target and proxy servers
27 exports.createServerPair = function (options, callback) {
30 // 1. Create the target server
32 function createTarget(next) {
33 exports.createServer(options.target, next);
36 // 2. Create the proxy server
38 function createTarget(next) {
39 http.createProxyServer(options.proxy, next);
45 // ### function createServer (options, callback)
46 // #### @options {Object} Options for creating the socket.io or ws server.
47 // #### @raw {boolean} Enables ws.Websocket server.
49 // Creates a socket.io or ws server using the specified `options`.
51 exports.createServer = function (options, callback) {
53 ? exports.createWsServer(options, callback)
54 : exports.createSocketIoServer(options, callback);
58 // ### function createSocketIoServer (options, callback)
59 // #### @options {Object} Options for creating the socket.io server
60 // #### @port {number} Port to listen on
61 // #### @input {string} Input to expect from the only socket
62 // #### @output {string} Output to send the only socket
64 // Creates a socket.io server on the specified `options.port` which
65 // will expect `options.input` and then send `options.output`.
67 exports.createSocketIoServer = function (options, callback) {
68 var server = protocols.target === 'https'
69 ? io.listen(options.port, helpers.https, callback)
70 : io.listen(options.port, callback);
72 server.sockets.on('connection', function (socket) {
73 socket.on('incoming', function (data) {
74 assert.equal(data, options.input);
75 socket.emit('outgoing', options.output);
81 // ### function createWsServer (options, callback)
82 // #### @options {Object} Options for creating the ws.Server instance
83 // #### @port {number} Port to listen on
84 // #### @input {string} Input to expect from the only socket
85 // #### @output {string} Output to send the only socket
87 // Creates a ws.Server instance on the specified `options.port` which
88 // will expect `options.input` and then send `options.output`.
90 exports.createWsServer = function (options, callback) {
94 if (protocols.target === 'https') {
95 server = https.createServer(helpers.https, function (req, res) {
98 }).listen(options.port, callback);
100 wss = new ws.Server({ server: server });
103 wss = new ws.Server({ port: options.port }, callback);
106 wss.on('connection', function (socket) {
107 socket.on('message', function (data) {
108 assert.equal(data, options.input);
109 socket.send(options.output);