-
Notifications
You must be signed in to change notification settings - Fork 50
/
index.js
75 lines (61 loc) · 1.42 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
* Module dependencies.
*/
const http = require('http');
/**
* Expose AGServer constructor.
*
* @api public
*/
module.exports.AGServer = require('./server');
/**
* Expose AGServerSocket constructor.
*
* @api public
*/
module.exports.AGServerSocket = require('./serversocket');
/**
* Expose AGRequest constructor.
*
* @api public
*/
module.exports.AGRequest = require('ag-request');
/**
* Creates an http.Server exclusively used for WS upgrades.
*
* @param {Number} port
* @param {Function} callback
* @param {Object} options
* @return {AGServer} websocket cluster server
* @api public
*/
module.exports.listen = function (port, options, fn) {
if (typeof options === 'function') {
fn = options;
options = {};
}
let server = http.createServer((req, res) => {
res.writeHead(501);
res.end('Not Implemented');
});
let socketClusterServer = module.exports.attach(server, options);
socketClusterServer.httpServer = server;
server.listen(port, fn);
return socketClusterServer;
};
/**
* Captures upgrade requests for a http.Server.
*
* @param {http.Server} server
* @param {Object} options
* @return {AGServer} websocket cluster server
* @api public
*/
module.exports.attach = function (server, options) {
if (options == null) {
options = {};
}
options.httpServer = server;
let socketClusterServer = new module.exports.AGServer(options);
return socketClusterServer;
};