-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwsServer.js
176 lines (153 loc) · 3.96 KB
/
wsServer.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
'use strict';
//websocket server manager
const WebSocketServer = require('ws').Server;
const logUtil = require('./log');
function resToMsg(msg, recorder, cb) {
let result = {},
jsonData;
try {
jsonData = JSON.parse(msg);
} catch (e) {
result = {
type: 'error',
error: 'failed to parse your request : ' + e.toString()
};
cb && cb(result);
return;
}
if (jsonData.reqRef) {
result.reqRef = jsonData.reqRef;
}
if (jsonData.type === 'reqBody' && jsonData.id) {
result.type = 'body';
recorder.getBody(jsonData.id, (err, data) => {
if (err) {
result.content = {
id: null,
body: null,
error: err.toString()
};
} else {
result.content = {
id: jsonData.id,
body: data.toString()
};
}
cb && cb(result);
});
} else { // more req handler here
return null;
}
}
//config.server
class wsServer {
constructor(config, recorder) {
if (!recorder) {
throw new Error('proxy recorder is required');
} else if (!config || !config.server) {
throw new Error('config.server is required');
}
const self = this;
self.config = config;
self.recorder = recorder;
}
start() {
const self = this;
const config = self.config;
const recorder = self.recorder;
return new Promise((resolve, reject) => {
//web socket interface
const wss = new WebSocketServer({
server: config.server,
clientTracking: true,
});
resolve();
// the queue of the messages to be delivered
let messageQueue = [];
// the flat to indicate wheter to broadcast the record
let broadcastFlag = true;
setInterval(() => {
broadcastFlag = true;
sendMultipleMessage();
}, 50);
function sendMultipleMessage(data) {
// if the flag goes to be true, and there are records to send
if (broadcastFlag && messageQueue.length > 0) {
wss && wss.broadcast({
type: 'updateMultiple',
content: messageQueue
});
messageQueue = [];
broadcastFlag = false;
} else {
data && messageQueue.push(data);
}
}
wss.broadcast = function (data) {
if (typeof data === 'object') {
try {
data = JSON.stringify(data);
} catch (e) {
console.error('==> errorr when do broadcast ', e, data);
}
}
for (const client of wss.clients) {
try {
client.send(data);
} catch (e) {
logUtil.printLog('websocket failed to send data, ' + e, logUtil.T_ERR);
}
}
};
wss.on('connection', (ws) => {
ws.on('message', (msg) => {
resToMsg(msg, recorder, (res) => {
res && ws.send(JSON.stringify(res));
});
});
ws.on('error', (e) => {
console.error('error in ws:', e);
});
});
wss.on('error', (e) => {
logUtil.printLog('websocket error, ' + e, logUtil.T_ERR);
});
wss.on('close', () => { });
recorder.on('update', (data) => {
try {
sendMultipleMessage(data);
} catch (e) {
console.log('ws error');
console.log(e);
}
});
recorder.on('updateLatestWsMsg', (data) => {
try {
// console.info('==> update latestMsg ', data);
wss && wss.broadcast({
type: 'updateLatestWsMsg',
content: data
});
} catch (e) {
logUtil.error(e.message);
logUtil.error(e.stack);
console.error(e);
}
});
self.wss = wss;
});
}
closeAll() {
const self = this;
return new Promise((resolve, reject) => {
self.wss.close((e) => {
if (e) {
reject(e);
} else {
resolve();
}
});
});
}
}
module.exports = wsServer;