2017-06-08 16:03:54 +00:00
|
|
|
import * as websocket from 'websocket';
|
|
|
|
import Xev from 'xev';
|
|
|
|
|
|
|
|
const ev = new Xev();
|
|
|
|
|
2018-03-07 02:40:40 +00:00
|
|
|
export default function(request: websocket.request, connection: websocket.connection): void {
|
2018-06-18 00:54:53 +00:00
|
|
|
const onStats = (stats: any) => {
|
2017-06-08 16:03:54 +00:00
|
|
|
connection.send(JSON.stringify({
|
|
|
|
type: 'stats',
|
|
|
|
body: stats
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
2018-06-08 16:45:25 +00:00
|
|
|
connection.on('message', async data => {
|
|
|
|
const msg = JSON.parse(data.utf8Data);
|
|
|
|
|
|
|
|
switch (msg.type) {
|
|
|
|
case 'requestLog':
|
2018-06-08 19:14:26 +00:00
|
|
|
ev.once('notesStatsLog:' + msg.id, statsLog => {
|
2018-06-08 16:45:25 +00:00
|
|
|
connection.send(JSON.stringify({
|
|
|
|
type: 'statsLog',
|
|
|
|
body: statsLog
|
|
|
|
}));
|
|
|
|
});
|
2018-06-08 19:14:26 +00:00
|
|
|
ev.emit('requestNotesStatsLog', msg.id);
|
2018-06-08 16:45:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-06-08 19:14:26 +00:00
|
|
|
ev.addListener('notesStats', onStats);
|
2017-06-08 16:03:54 +00:00
|
|
|
|
|
|
|
connection.on('close', () => {
|
2018-06-08 19:14:26 +00:00
|
|
|
ev.removeListener('notesStats', onStats);
|
2017-06-08 16:03:54 +00:00
|
|
|
});
|
|
|
|
}
|