diff options
Diffstat (limited to 'src/server/api/stream/server-stats.ts')
| -rw-r--r-- | src/server/api/stream/server-stats.ts | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/server/api/stream/server-stats.ts b/src/server/api/stream/server-stats.ts new file mode 100644 index 0000000000..342170a21e --- /dev/null +++ b/src/server/api/stream/server-stats.ts @@ -0,0 +1,35 @@ +import * as websocket from 'websocket'; +import Xev from 'xev'; + +const ev = new Xev(); + +export default function(request: websocket.request, connection: websocket.connection): void { + const onStats = stats => { + connection.send(JSON.stringify({ + type: 'stats', + body: stats + })); + }; + + connection.on('message', async data => { + const msg = JSON.parse(data.utf8Data); + + switch (msg.type) { + case 'requestLog': + ev.once('serverStatsLog:' + msg.id, statsLog => { + connection.send(JSON.stringify({ + type: 'statsLog', + body: statsLog + })); + }); + ev.emit('requestServerStatsLog', msg.id); + break; + } + }); + + ev.addListener('serverStats', onStats); + + connection.on('close', () => { + ev.removeListener('serverStats', onStats); + }); +} |