Fix issues with websocket communication on status change

This commit is contained in:
David Majdandžić
2023-03-24 16:45:44 +01:00
parent ca50864142
commit 5dc0e36818
2 changed files with 9 additions and 7 deletions

14
main.js
View File

@@ -467,20 +467,21 @@ class WSServer {
if (index > -1) { if (index > -1) {
delete this.clients[sessionId][index]; delete this.clients[sessionId][index];
} }
this.clients[sessionId] = this.clients[sessionId].filter(Boolean);
} }
} }
onSessionChange(sessionId, session) { onSessionChange(sessionId, newStatus) {
// TODO: Also maybe create a broadcast for any pdu // TODO: Also maybe create a broadcast for any pdu
// To do this add ssomething to the client message maybe like sessionId:listenToPdu? // To do this add ssomething to the client message maybe like sessionId:listenToPdu?
// So something like 0:1 or 0:true // So something like 0:1 or 0:true
// Then send any pdu updates to all clients with listen to true // Then send any pdu updates to all clients with listen to true
this.logger.log1(`Session with ID ${sessionId} changed`); this.logger.log1(`Session with ID ${sessionId} changed`);
if (this.clients[sessionId]) { let clients = this.clients[sessionId];
this.logger.log1(`Broadcasting session with ID ${sessionId} to ${this.clients[sessionId].length} clients`) if (!!clients) {
let value = session.serialize(); this.logger.log1(`Broadcasting session with ID ${sessionId} to ${clients.length} clients`)
this.clients[sessionId].forEach(client => { clients.forEach(client => {
client.send(JSON.stringify(value)); client.send(newStatus);
}); });
} }
} }
@@ -494,5 +495,6 @@ function sleep(ms) {
let sessionManager = new SessionManager(); let sessionManager = new SessionManager();
let session = sessionManager.createSession('localhost:7001', 'test', 'test'); let session = sessionManager.createSession('localhost:7001', 'test', 'test');
// session.connect();
new WSServer(); new WSServer();
new HTTPServer(); new HTTPServer();

View File

@@ -8,5 +8,5 @@ ws.on('open', () => {
ws.send(0); ws.send(0);
}); });
ws.on('message', (data) => { ws.on('message', (data) => {
console.log(data); console.log(String(data));
}); });