Repository navigation
Expand file tree
/
Copy pathdstat.js
More file actions
51 lines (42 loc) · 1.38 KB
/
Copy pathdstat.js
File metadata and controls
51 lines (42 loc) · 1.38 KB
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
const express = require('express');
const WebSocket = require('ws');
const fs = require('fs');
var rps = 0;
setInterval(() => rps = 0, 1000);
class Dstat {
constructor(options) {
this.options = options;
this.express = express;
this.app = this.express();
this.wss = new WebSocket.Server(options.websocket);
this.js = fs.readFileSync('html/js/index.js', 'utf-8').replace(/{port}/gi, options.websocket.port);
}
start() {
this.startSite();
this.startDstat();
}
startSite() {
this.app.use((req, res, next) => {
rps++;
next();
});
this.app.get('/js/index.js', (req, res) => res.end(this.js));
this.app.use(this.express.static('html'));
this.app.listen(this.options.site.port, () => {
console.log('Started L7 Dstat on %s', this.options.site.port);
});
}
startDstat() {
this.wss.on('connection', function connection(ws) {
let int = setInterval(() => send({ requests: rps }), 1000);
function send(json) {
if (ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify(json));
} else if (int) {
clearInterval(int);
}
}
});
}
}
module.exports = Dstat;