/
server.js
57 lines (47 loc) · 1.36 KB
/
server.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
var sys = require('sys');
var http = require('http');
var repl = require('repl');
var serverPort = 8080;
var connCount = 0;
var connCountTotal = 0;
var countReportDelay = 5 * 1000;
var resDelay = 15 * 1000;
function handleConnClose () {
connCount--;
}
function handleConnOpen (conn) {
connCountTotal++;
connCount++;
conn.addListener("close", handleConnClose);
}
function formatConnStat () { return("NOW: " + Math.round(new Date().getTime() / 1000) + " | OC: " + connCount + " | TC: " + connCountTotal); }
function connStat () {
sys.puts(formatConnStat());
}
function handleRequest (req, res) {
res.sendHeader(200, {'Content-Type': 'text/plain'});
res.sendBody('x');
res.finish();
}
process.addListener("SIGINT", function () {
sys.puts("Quitting. " + connCountTotal + " connections received");
process.exit(0);
});
var server = http.createServer(function(req, res) {
if (req.uri.full.toLowerCase() == "/cstat") {
res.sendHeader(200, {'Content-Type': 'text/plain'});
res.sendBody(formatConnStat());
res.finish();
} else {
setTimeout(function () { handleRequest(req, res); }, resDelay)
}
})
server.addListener("connection", handleConnOpen);
server.listen(8080);
//*/
setInterval(connStat, countReportDelay);
/*/
repl.scope.cstat = connStat;
repl.start();
//*/
sys.puts("Listening on port " + serverPort + " with a response delay of " + (resDelay / 1000) + "sec");