From c78945436e39121f46e083eac7a6572ca2efe2d2 Mon Sep 17 00:00:00 2001 From: syuilo Date: Sat, 9 Jun 2018 04:14:26 +0900 Subject: #1686 --- src/server/api/stream/notes-stats.ts | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/server/api/stream/notes-stats.ts (limited to 'src/server/api/stream/notes-stats.ts') diff --git a/src/server/api/stream/notes-stats.ts b/src/server/api/stream/notes-stats.ts new file mode 100644 index 0000000000..739b325848 --- /dev/null +++ b/src/server/api/stream/notes-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('notesStatsLog:' + msg.id, statsLog => { + connection.send(JSON.stringify({ + type: 'statsLog', + body: statsLog + })); + }); + ev.emit('requestNotesStatsLog', msg.id); + break; + } + }); + + ev.addListener('notesStats', onStats); + + connection.on('close', () => { + ev.removeListener('notesStats', onStats); + }); +} -- cgit v1.2.3-freya