using SSE instead of socket IO

This commit is contained in:
Jan Prochazka
2021-12-22 15:14:52 +01:00
parent 2ace0bdb34
commit 318b137490
7 changed files with 91 additions and 43 deletions

View File

@@ -1,19 +1,35 @@
let socket = null;
let res = null;
let init = '';
module.exports = {
set(value) {
socket = value;
},
get() {
return socket;
res = value;
},
// get() {
// return socket;
// },
emit(message, data) {
if (res) {
if (init) {
res.write(init);
init = '';
}
res.write(`event: ${message}\ndata: ${JSON.stringify(data == null ? null : data)}\n\n`);
} else {
init += res;
}
// console.log('EMIT:', message, data);
socket.emit(message, data);
// socket.emit(message, data);
},
emitChanged(key) {
this.emit('clean-cache', key);
this.emit(key);
// console.log('EMIT_CHANGED:', key);
socket.emit('clean-cache', key);
socket.emit(key);
// socket.emit('clean-cache', key);
// socket.emit(key);
// socket.send(key, 'clean-cache');
// socket.send(null, key);
},
};