Fix issue where websockets would be sent duplicate messages

This commit is contained in:
David Majdandžić
2023-03-26 18:14:06 +02:00
parent 268d19815a
commit 190ae7e296
2 changed files with 28 additions and 15 deletions

17
main.js
View File

@@ -1417,6 +1417,7 @@ class HTTPServer {
class WSServer { class WSServer {
clients = {}; clients = {};
unknownClients = []; unknownClients = [];
listenersAlreadySetup = [];
constructor() { constructor() {
this.server = new WebSocket.Server({port: WS_SERVER_PORT}); this.server = new WebSocket.Server({port: WS_SERVER_PORT});
@@ -1439,23 +1440,36 @@ class WSServer {
if (!this.clients[type][sessionId]) { if (!this.clients[type][sessionId]) {
this.clients[type][sessionId] = []; this.clients[type][sessionId] = [];
} }
this.logger.log1(`Adding client ${ws.id} to ${type} session ${sessionId}`);
if (type === "client") { if (type === "client") {
if (this.listenersAlreadySetup.indexOf(`client-${sessionId}`) === -1) {
let session = clientSessionManager.getSession(sessionId); let session = clientSessionManager.getSession(sessionId);
if (!!session) { if (!!session) {
this.logger.log1(`Setting up listeners for client session ${sessionId}`);
session.on(ClientSession.STATUS_CHANGED_EVENT, this.onClientSessionStatusChange.bind(this, sessionId)); session.on(ClientSession.STATUS_CHANGED_EVENT, this.onClientSessionStatusChange.bind(this, sessionId));
session.on(ClientSession.ANY_PDU_EVENT, this.onClientSessionPdu.bind(this, sessionId)); session.on(ClientSession.ANY_PDU_EVENT, this.onClientSessionPdu.bind(this, sessionId));
session.on(ClientSession.MESSAGE_SEND_COUNTER_UPDATE_EVENT, this.onClientMessageCounterUpdate.bind(this, sessionId)); session.on(ClientSession.MESSAGE_SEND_COUNTER_UPDATE_EVENT, this.onClientMessageCounterUpdate.bind(this, sessionId));
} }
this.listenersAlreadySetup.push(`client-${sessionId}`);
} else {
this.logger.log1(`Listeners for client session ${sessionId} already set up`);
}
} else if (type === "center") { } else if (type === "center") {
if (this.listenersAlreadySetup.indexOf(`center-${sessionId}`) === -1) {
let session = centerSessionManager.getSession(sessionId); let session = centerSessionManager.getSession(sessionId);
if (!!session) { if (!!session) {
this.logger.log1(`Setting up listeners for center session ${sessionId}`);
session.on(CenterSession.STATUS_CHANGED_EVENT, this.onCenterStatusChange.bind(this, sessionId)); session.on(CenterSession.STATUS_CHANGED_EVENT, this.onCenterStatusChange.bind(this, sessionId));
session.on(CenterSession.ANY_PDU_EVENT, this.onCenterServerPdu.bind(this, sessionId)); session.on(CenterSession.ANY_PDU_EVENT, this.onCenterServerPdu.bind(this, sessionId));
session.on(CenterSession.MODE_CHANGED_EVENT, this.onCenterModeChanged.bind(this, sessionId)); session.on(CenterSession.MODE_CHANGED_EVENT, this.onCenterModeChanged.bind(this, sessionId));
session.on(CenterSession.SESSION_CHANGED_EVENT, this.onCenterSessionsChanged.bind(this, sessionId)); session.on(CenterSession.SESSION_CHANGED_EVENT, this.onCenterSessionsChanged.bind(this, sessionId));
session.on(ClientSession.MESSAGE_SEND_COUNTER_UPDATE_EVENT, this.onCenterMessageCounterUpdate.bind(this, sessionId)); session.on(ClientSession.MESSAGE_SEND_COUNTER_UPDATE_EVENT, this.onCenterMessageCounterUpdate.bind(this, sessionId));
} }
this.listenersAlreadySetup.push(`center-${sessionId}`);
} else {
this.logger.log1(`Listeners for center session ${sessionId} already set up`);
}
} }
this.clients[type][sessionId].push(ws); this.clients[type][sessionId].push(ws);
@@ -1535,7 +1549,6 @@ class WSServer {
value: pdu value: pdu
} }
this.logger.log2(`Broadcasting session with ID ${sessionId} to ${clients.length} clients`); this.logger.log2(`Broadcasting session with ID ${sessionId} to ${clients.length} clients`);
console.log(clients);
clients.forEach(client => { clients.forEach(client => {
client.send(JSON.stringify(payload)); client.send(JSON.stringify(payload));
}); });
@@ -1663,7 +1676,7 @@ let server = centerSessionManager.getSession(1);
session.connect() session.connect()
.then(() => { .then(() => {
session.bind().then(() => { session.bind().then(() => {
setTimeout(() => session.close(), 1000); // setTimeout(() => session.close(), 1000);
}).catch(err => console.log(err)); }).catch(err => console.log(err));
}).catch(err => console.log(err)); }).catch(err => console.log(err));

View File

@@ -13,7 +13,7 @@ ws.on('message', (data) => {
}); });
ws2.on('open', () => { ws2.on('open', () => {
console.log('WebSocket connection established'); console.log('WebSocket connection established');
ws.send("center:0"); ws.send("center:1");
}); });
ws2.on('message', (data) => { ws2.on('message', (data) => {
console.log(String(data)); console.log(String(data));