-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalert-overlay.js
More file actions
37 lines (31 loc) · 937 Bytes
/
alert-overlay.js
File metadata and controls
37 lines (31 loc) · 937 Bytes
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
const express = require('express');
const expressws = require('express-ws');
class AlertOverlay {
constructor(cfg) {
this._app = express();
this._ws = expressws(this._app);
this._clients = [];
this._app.use(express.static('www'));
this._app.ws('/', (ws, req) => {
let index = this._clients.push(ws) - 1;
ws.on('close', (ws) => {
this._clients.splice(index, 1);
});
});
this._app.listen(cfg['overlay-port']);
}
notify(params) {
const message = {
user: params.user,
text: params.text
}
const promises = this._clients.map(async (ws) => {
ws.send(JSON.stringify(message));
});
return new Promise(async resolve => {
await Promise.all(promises);
resolve();
});
}
}
module.exports = exports = AlertOverlay;