-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
54 lines (41 loc) · 1.57 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
/**
* Initiate app server
* @module
*/
var ranger = require('park-ranger')();
var app = require('app');
var debug = require('app/lib/debug')('app:server');
var http = require('http');
var https = require('https');
var passportSocketIO = require('app/lib/passportSocketIO');
var socketEvents = require('app/socketEvents');
var socketIO = require('socket.io');
var httpPort = process.env.SYNC_SERVER_HTTP_PORT ? process.env.SYNC_SERVER_HTTP_PORT : 9001;
var httpsPort = process.env.SYNC_SERVER_HTTPS_PORT ? process.env.SYNC_SERVER_HTTPS_PORT : 9002;
var httpServer = http.createServer(app).listen(httpPort, () => {
debug('App server started listening for HTTP requests', { port: httpPort });
});
var httpsServer = https.createServer(ranger.cert, app).listen(httpsPort, () => {
debug('App server started listening for HTTPS requests', { port: httpsPort });
});
var servers = {
'https': httpsServer,
'http': httpServer
};
Object.keys(servers).forEach((key) => {
var server = servers[key];
server.io = socketIO(server);
server.io.on('connection', (socket) => {
debug('opened socket.io connection for %s', key);
var listeners = socketEvents(server, socket);
debug('listeners count for %s: %s', key, Object.keys(listeners).length);
socket.on('disconnect', () => {
debug('closed socket.io connection for %s', key);
Object.keys(listeners).forEach(function(key) {
app.removeListener(key, listeners[key]);
});
});
});
server.io.use(passportSocketIO);
debug('App server started listening for WebSocket connections for %s', key);
});