-
Notifications
You must be signed in to change notification settings - Fork 0
/
HttpServer.js
63 lines (46 loc) · 1.52 KB
/
HttpServer.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
/* HttpServer.js
FPI, 2021-09-15
*/
const http = require('http');
class HttpServer {
/*
*/
static log( funcName, txt, level) {
console.log( new Date().yyyymmddhhmmsslll() + ` > HttpServer > ${funcName} > ${txt}`);
}
/*
*/
static Initialize( httpRequestHandler, callBackInitialized) {
const funcName = "Initialize";
let logDebug = false;
HttpServer.httpPort = 9080;
HttpServer.log( funcName, `start http server on ${HttpServer.httpPort}...`, "info");
let httpServer = http.createServer( httpRequestHandler);
httpServer.listen( HttpServer.httpPort, (err) => {
if (err) {
HttpServer.log( funcName, `listen > something bad happened > ${err}`, 'warn');
// console.debug( `listen > something bad happened > ${err}`);
return;
}
HttpServer.log( funcName, "listen > server is listening", 'info');
if (callBackInitialized) callBackInitialized(undefined);
});
HttpServer.sockets = {};
HttpServer.nextSocketId = 0;
httpServer.on( 'connection', function( socket) {
// Add a newly connected socket
HttpServer.nextSocketId ++;
let socketId = HttpServer.nextSocketId;
HttpServer.sockets[ socketId] = socket;
if (logDebug)
HttpServer.log( funcName, `on connection > socket ${socketId} opened`, 'info');
// Remove the socket when it closes
socket.on( 'close', function () {
if (logDebug)
HttpServer.log( funcName, ` on close > socket ${socketId} closed`, 'info');
delete HttpServer.sockets[ socketId];
});
});
}
}
module.exports = HttpServer;