add error notice on online counter broadcast

This commit is contained in:
HF 2022-07-02 13:24:09 +02:00
parent 214daa6382
commit eee3d6a7ec
2 changed files with 36 additions and 28 deletions

View File

@ -207,11 +207,15 @@ class SocketClient extends EventEmitter {
this.emit('pixelUpdate', PixelUpdate.hydrate(data)); this.emit('pixelUpdate', PixelUpdate.hydrate(data));
break; break;
case PixelReturn.OP_CODE: case PixelReturn.OP_CODE:
/*
* using online counter and pxlReturn as sign-of-life ping
*/
this.timeLastPing = Date.now();
this.emit('pixelReturn', PixelReturn.hydrate(data)); this.emit('pixelReturn', PixelReturn.hydrate(data));
break; break;
case OnlineCounter.OP_CODE: case OnlineCounter.OP_CODE:
/* /*
* using online counter as sign-of-life ping * using online counter and pxlReturn as sign-of-life ping
*/ */
this.timeLastPing = Date.now(); this.timeLastPing = Date.now();
this.emit('onlineCounter', OnlineCounter.hydrate(data)); this.emit('onlineCounter', OnlineCounter.hydrate(data));

View File

@ -361,36 +361,40 @@ class SocketServer {
} }
onlineCounterBroadcast() { onlineCounterBroadcast() {
const online = {}; try {
online.total = ipCounter.amount() || 0; const online = {};
const ipsPerCanvas = {}; online.total = ipCounter.amount() || 0;
const it = this.wss.clients.keys(); const ipsPerCanvas = {};
let client = it.next(); const it = this.wss.clients.keys();
while (!client.done) { let client = it.next();
const ws = client.value; while (!client.done) {
if (ws.readyState === WebSocket.OPEN const ws = client.value;
&& ws.user if (ws.readyState === WebSocket.OPEN
) { && ws.user
const canvasId = ws.canvasId || 0; ) {
const { ip } = ws.user; const canvasId = ws.canvasId || 0;
// only count unique IPs per canvas const { ip } = ws.user;
if (!ipsPerCanvas[canvasId]) { // only count unique IPs per canvas
ipsPerCanvas[canvasId] = [ip]; if (!ipsPerCanvas[canvasId]) {
} else if (ipsPerCanvas[canvasId].includes(ip)) { ipsPerCanvas[canvasId] = [ip];
client = it.next(); } else if (ipsPerCanvas[canvasId].includes(ip)) {
continue; client = it.next();
} else { continue;
ipsPerCanvas[canvasId].push(ip); } else {
ipsPerCanvas[canvasId].push(ip);
}
//--
if (!online[canvasId]) {
online[canvasId] = 0;
}
online[canvasId] += 1;
} }
//-- client = it.next();
if (!online[canvasId]) {
online[canvasId] = 0;
}
online[canvasId] += 1;
} }
client = it.next(); socketEvents.broadcastOnlineCounter(online);
} catch (err) {
logger.error(`WebSocket online broadcast error: ${err.message}`);
} }
socketEvents.broadcastOnlineCounter(online);
} }
async onTextMessage(text, ws) { async onTextMessage(text, ws) {