-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
87 lines (64 loc) · 2.07 KB
/
app.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
76
77
78
79
80
81
82
83
84
85
86
87
/**
* Module dependencies.
*/
import fs from 'fs';
import http from 'http';
import express from'express';
import program from 'commander';
import config from 'config';
import WsServer from '~/ws_server';
program
.option('-p, --port [port]', 'Listening port')
.parse(process.argv);
/**
* Middlewares.
*/
import logger from '~/lib/middlewares/socket/logger';
import cookieParser from '~/lib/middlewares/socket/cookie_parser';
import session from '~/lib/middlewares/socket/session';
import authentication from '~/lib/middlewares/socket/authentication';
/**
* HTTP Server Settings.
*/
const app = express();
app.get('/health_check', (request, response) => {
response.send('ok');
});
/**
* WsServer Settings.
*/
const server = http.Server(app);
const wsServerConfig = config.get('wsServer');
const wsServer = new WsServer(server, {
transports: wsServerConfig.transports
});
// Redis setting is used only for Pub / Sub.s
const redisWsServer = WsServer.getRedisWsServer(config.get('datastore.redisWsServers'), process.env['WsGroup']);
wsServer.setRedisAdapter({
host: redisWsServer.host,
port: redisWsServer.port
});
wsServer.setSocketMiddleware(cookieParser, '/');
wsServer.setSocketMiddleware(session, '/');
// Used when authentication is required.
// wsServer.setSocketMiddleware(authentication, '/');
fs.readdirSync(process.cwd() + wsServerConfig.eventsDirPath)
.forEach(fileNameWithExtension => {
// Remove extension
const fileName = fileNameWithExtension.replace(/\.[a-zA-Z]*$/, '');
const namespace = fileName === 'index' ? '/' : '/' + fileName;
wsServer.setSocketMiddleware(logger, namespace);
wsServer.io
.of(namespace)
.on('connect', (socket) => {
// connect log
socket.logger.access('connect ' + namespace, '-');
// event log
socket.use((packet, callback) => {
socket.logger.access(packet[0], packet[1]);
callback();
});
wsServer.setEvent(socket, wsServerConfig.eventsDirPath + '/' + fileName);
});
});
server.listen(program.port || wsServerConfig.port);