2017-06-09 01:03:54 +09:00
|
|
|
import * as websocket from 'websocket';
|
|
|
|
import Xev from 'xev';
|
|
|
|
|
|
|
|
const ev = new Xev();
|
|
|
|
|
2018-03-07 11:40:40 +09:00
|
|
|
export default function(request: websocket.request, connection: websocket.connection): void {
|
2018-06-18 09:54:53 +09:00
|
|
|
const onStats = (stats: any) => {
|
2017-06-09 01:03:54 +09:00
|
|
|
connection.send(JSON.stringify({
|
|
|
|
type: 'stats',
|
|
|
|
body: stats
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
2018-06-09 01:45:25 +09:00
|
|
|
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);
|
2017-06-09 01:03:54 +09:00
|
|
|
|
|
|
|
connection.on('close', () => {
|
2018-06-09 01:45:25 +09:00
|
|
|
ev.removeListener('serverStats', onStats);
|
2017-06-09 01:03:54 +09:00
|
|
|
});
|
|
|
|
}
|