engine.io.js (2378B)
1 /** 2 * Module dependencies. 3 */ 4 5 var http = require('http'); 6 7 /** 8 * Invoking the library as a function delegates to attach if the first argument 9 * is an `http.Server`. 10 * 11 * If there are no arguments or the first argument is an options object, then 12 * a new Server instance is returned. 13 * 14 * @param {http.Server} server (if specified, will be attached to by the new Server instance) 15 * @param {Object} options 16 * @return {Server} engine server 17 * @api public 18 */ 19 20 exports = module.exports = function () { 21 // backwards compatible use as `.attach` 22 // if first argument is an http server 23 if (arguments.length && arguments[0] instanceof http.Server) { 24 return attach.apply(this, arguments); 25 } 26 27 // if first argument is not an http server, then just make a regular eio server 28 return exports.Server.apply(null, arguments); 29 }; 30 31 /** 32 * Protocol revision number. 33 * 34 * @api public 35 */ 36 37 exports.protocol = 1; 38 39 /** 40 * Expose Server constructor. 41 * 42 * @api public 43 */ 44 45 exports.Server = require('./server'); 46 47 /** 48 * Expose Socket constructor. 49 * 50 * @api public 51 */ 52 53 exports.Socket = require('./socket'); 54 55 /** 56 * Expose Transport constructor. 57 * 58 * @api public 59 */ 60 61 exports.Transport = require('./transport'); 62 63 /** 64 * Expose mutable list of available transports. 65 * 66 * @api public 67 */ 68 69 exports.transports = require('./transports'); 70 71 /** 72 * Exports parser. 73 * 74 * @api public 75 */ 76 77 exports.parser = require('engine.io-parser'); 78 79 /** 80 * Creates an http.Server exclusively used for WS upgrades. 81 * 82 * @param {Number} port 83 * @param {Function} callback 84 * @param {Object} options 85 * @return {Server} websocket.io server 86 * @api public 87 */ 88 89 exports.listen = listen; 90 91 function listen (port, options, fn) { 92 if ('function' === typeof options) { 93 fn = options; 94 options = {}; 95 } 96 97 var server = http.createServer(function (req, res) { 98 res.writeHead(501); 99 res.end('Not Implemented'); 100 }); 101 102 // create engine server 103 var engine = exports.attach(server, options); 104 engine.httpServer = server; 105 106 server.listen(port, fn); 107 108 return engine; 109 } 110 111 /** 112 * Captures upgrade requests for a http.Server. 113 * 114 * @param {http.Server} server 115 * @param {Object} options 116 * @return {Server} engine server 117 * @api public 118 */ 119 120 exports.attach = attach; 121 122 function attach (server, options) { 123 var engine = new exports.Server(options); 124 engine.attach(server, options); 125 return engine; 126 }