forked from ardatan/whatwg-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uwsUtils.js
40 lines (38 loc) · 862 Bytes
/
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
const uws = require('uWebSockets.js');
let handler;
let uwsApp = uws.App().any('/*', (...args) => {
const res = handler(...args);
handler = undefined;
return res;
});
let listenSocket;
module.exports = {
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);
}
},
get port() {
if (listenSocket) {
return uws.us_socket_local_port(listenSocket);
}
},
addOnceHandler(newHandler) {
handler = newHandler;
},
};