mirror of
https://git.joinsharkey.org/Sharkey/Sharkey.git
synced 2024-11-08 21:03:09 +02:00
35 lines
768 B
TypeScript
35 lines
768 B
TypeScript
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: any) => {
|
|
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);
|
|
});
|
|
}
|