This commit is contained in:
35
src/server/api/stream/notes-stats.ts
Normal file
35
src/server/api/stream/notes-stats.ts
Normal file
@ -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);
|
||||
});
|
||||
}
|
@ -12,7 +12,8 @@ import messagingStream from './stream/messaging';
|
||||
import messagingIndexStream from './stream/messaging-index';
|
||||
import othelloGameStream from './stream/othello-game';
|
||||
import othelloStream from './stream/othello';
|
||||
import serverStream from './stream/server';
|
||||
import serverStatsStream from './stream/server-stats';
|
||||
import notesStatsStream from './stream/notes-stats';
|
||||
import requestsStream from './stream/requests';
|
||||
import { ParsedUrlQuery } from 'querystring';
|
||||
import authenticate from './authenticate';
|
||||
@ -28,8 +29,13 @@ module.exports = (server: http.Server) => {
|
||||
ws.on('request', async (request) => {
|
||||
const connection = request.accept();
|
||||
|
||||
if (request.resourceURL.pathname === '/server') {
|
||||
serverStream(request, connection);
|
||||
if (request.resourceURL.pathname === '/server-stats') {
|
||||
serverStatsStream(request, connection);
|
||||
return;
|
||||
}
|
||||
|
||||
if (request.resourceURL.pathname === '/notes-stats') {
|
||||
notesStatsStream(request, connection);
|
||||
return;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user