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