mizzkey/src/server/api/stream/server-stats.ts

39 lines
814 B
TypeScript
Raw Normal View History

2017-06-08 18:03:54 +02:00
import * as websocket from 'websocket';
import Xev from 'xev';
const ev = new Xev();
2018-03-07 03:40:40 +01:00
export default function(request: websocket.request, connection: websocket.connection): void {
2018-06-18 02:54:53 +02:00
const onStats = (stats: any) => {
2017-06-08 18:03:54 +02:00
connection.send(JSON.stringify({
type: 'stats',
body: stats
}));
};
connection.on('message', async data => {
const msg = JSON.parse(data.utf8Data);
switch (msg.type) {
case 'requestLog':
2018-09-01 16:12:51 +02:00
ev.once(`serverStatsLog:${msg.id}`, statsLog => {
connection.send(JSON.stringify({
type: 'statsLog',
body: statsLog
}));
});
ev.emit('requestServerStatsLog', {
id: msg.id,
length: msg.length
});
break;
}
});
ev.addListener('serverStats', onStats);
2017-06-08 18:03:54 +02:00
connection.on('close', () => {
ev.removeListener('serverStats', onStats);
2017-06-08 18:03:54 +02:00
});
}