-
Notifications
You must be signed in to change notification settings - Fork 20
/
app.js
50 lines (38 loc) · 1.21 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
'use strict';
const WebSocket = require('ws');
class XtransitServerBoot {
constructor(app) {
this.app = app;
}
async serverDidReady() {
const { app, app: { server, logger: appLogger } } = this;
const wss = new WebSocket.Server({ server });
wss.on('connection', (ws, req) => {
const { service: { handler }, logger } = app.createAnonymousContext();
// check valid
handler.checkValid(ws);
// handle message event
ws.on('message', message =>
handler
.message(ws, message)
.catch(err => logger.error(`ws handle message error: ${err}, message: ${message}.`)));
// handle close event
ws.on('close', () =>
handler
.close(ws, req)
.catch(err => logger.error(`ws handle close error: ${err}.`)));
// handle error event
ws.on('error', err => {
logger.error(`ws handle receive error: ${err}.`);
});
});
wss.on('error', err => appLogger.error(`websocket server error: ${err}.`));
}
async didReady() {
const { app } = this;
const { service: { ipc } } = app.createAnonymousContext();
// register ipc channel
ipc.register();
}
}
module.exports = XtransitServerBoot;