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));
break;
case PixelReturn.OP_CODE:
/*
* using online counter and pxlReturn as sign-of-life ping
*/
this.timeLastPing = Date.now();
this.emit('pixelReturn', PixelReturn.hydrate(data));
break;
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.emit('onlineCounter', OnlineCounter.hydrate(data));

View File

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