fix ids and online
This commit is contained in:
parent
45e7298bca
commit
465b78627e
26
src/index.js
26
src/index.js
|
@ -75,6 +75,7 @@ const server = new WebSocket.Server({
|
|||
|
||||
let clients = [];
|
||||
let lastPixelTimestamp = [];
|
||||
let lastId = 1;
|
||||
|
||||
const evaulatePixelNumber = (x, y) => {
|
||||
let pixelNumber;
|
||||
|
@ -84,10 +85,23 @@ const evaulatePixelNumber = (x, y) => {
|
|||
pixelNumber = x;
|
||||
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) => {
|
||||
client.id = clients.length;
|
||||
client.id = lastId;
|
||||
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) {
|
||||
let packet, content, code;
|
||||
|
@ -101,15 +115,11 @@ server.on('connection', (client) => {
|
|||
case 0:
|
||||
response.code = 0;
|
||||
response.content = board;
|
||||
|
||||
response.timeBetweenPixels = timeBetweenPixels;
|
||||
client.send(Buffer.from(JSON.stringify(response)));
|
||||
response = {}
|
||||
for (let i = 0; i < clients.length; i++) {
|
||||
response.code = 2;
|
||||
response.online = clients.length;
|
||||
clients[i].send(Buffer.from(JSON.stringify(response)));
|
||||
}
|
||||
|
||||
boradcastOnline();
|
||||
break;
|
||||
case 1:
|
||||
response.code = 1;
|
||||
|
|
Loading…
Reference in New Issue