-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
uwsUtils.js
44 lines (43 loc) · 1.04 KB
/
uwsUtils.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
const uws = require('uWebSockets.js');
module.exports = {
createUWS() {
let handler;
let uwsApp = uws.App().any('/*', (...args) => {
const res = handler(...args);
handler = undefined;
return res;
});
let listenSocket;
return {
getApp() {
return uwsApp;
},
start() {
return new Promise(function (resolve, reject) {
uwsApp.listen(0, function (newListenSocket) {
if (newListenSocket) {
listenSocket = newListenSocket;
resolve(uws.us_socket_local_port(listenSocket));
} else {
reject(new Error('uWS App cannot start'));
}
});
});
},
stop() {
if (listenSocket) {
uws.us_listen_socket_close(listenSocket);
uwsApp.close();
}
},
get port() {
if (listenSocket) {
return uws.us_socket_local_port(listenSocket);
}
},
addOnceHandler(newHandler) {
handler = newHandler;
},
};
},
};