fix ids and online

This commit is contained in:
leca 2024-04-08 00:29:56 +03:00
parent 45e7298bca
commit 465b78627e
1 changed files with 18 additions and 8 deletions

View File

@ -75,6 +75,7 @@ const server = new WebSocket.Server({
let clients = []; let clients = [];
let lastPixelTimestamp = []; let lastPixelTimestamp = [];
let lastId = 1;
const evaulatePixelNumber = (x, y) => { const evaulatePixelNumber = (x, y) => {
let pixelNumber; let pixelNumber;
@ -84,10 +85,23 @@ const evaulatePixelNumber = (x, y) => {
pixelNumber = x; pixelNumber = x;
return pixelNumber; return pixelNumber;
} }
const boradcastOnline = () => {
let response = {}
for (let i = 0; i < clients.length; i++) {
response.code = 2;
response.online = clients.length;
clients[i].send(Buffer.from(JSON.stringify(response)));
}
}
setInterval(boradcastOnline, 15000);
server.on('connection', (client) => { server.on('connection', (client) => {
client.id = clients.length; client.id = lastId;
clients.push(client); clients.push(client);
console.log(`New client has connected, assigning id ${clients.length} to it.`) lastId ++;
console.log(`New client has connected, assigning id ${client.id} to it.`)
client.on('message', function (msg) { client.on('message', function (msg) {
let packet, content, code; let packet, content, code;
@ -101,15 +115,11 @@ server.on('connection', (client) => {
case 0: case 0:
response.code = 0; response.code = 0;
response.content = board; response.content = board;
response.timeBetweenPixels = timeBetweenPixels; response.timeBetweenPixels = timeBetweenPixels;
client.send(Buffer.from(JSON.stringify(response))); client.send(Buffer.from(JSON.stringify(response)));
response = {} response = {}
for (let i = 0; i < clients.length; i++) { boradcastOnline();
response.code = 2;
response.online = clients.length;
clients[i].send(Buffer.from(JSON.stringify(response)));
}
break; break;
case 1: case 1:
response.code = 1; response.code = 1;