This commit is contained in:
David Majdandžić
2023-03-24 16:38:45 +01:00
parent 5ac4a6275b
commit ca50864142
2 changed files with 25 additions and 34 deletions

57
main.js
View File

@@ -131,6 +131,9 @@ class Session {
reject: null reject: null
} }
static STATUS_CHANGED_EVENT = "statusChanged";
static ANY_PDU_EVENT = "*";
constructor(id, url, username, password) { constructor(id, url, username, password) {
this.id = id; this.id = id;
this.logger = new Logger(`Session-${this.id}`); this.logger = new Logger(`Session-${this.id}`);
@@ -146,7 +149,7 @@ class Session {
setStatus(newStatus) { setStatus(newStatus) {
this.status = newStatus; this.status = newStatus;
this.eventEmitter.emit("statusChanged", newStatus); this.eventEmitter.emit(Session.STATUS_CHANGED_EVENT, newStatus);
} }
connect() { connect() {
@@ -167,7 +170,7 @@ class Session {
} catch (e) { } catch (e) {
this.logger.log1("Connection failed to " + this.url); this.logger.log1("Connection failed to " + this.url);
this.setStatus(SessionStatus.CONNECT_FAILED); this.setStatus(SessionStatus.CONNECT_FAILED);
console.log(e); reject("Connection failed to " + this.url);
} }
this.connectingPromise.resolve = resolve; this.connectingPromise.resolve = resolve;
this.connectingPromise.reject = reject; this.connectingPromise.reject = reject;
@@ -191,7 +194,7 @@ class Session {
this.session.on('debug', (type, msg, payload) => { this.session.on('debug', (type, msg, payload) => {
if (type.includes('pdu.')) { if (type.includes('pdu.')) {
this.eventEmitter.emit(msg, payload); this.eventEmitter.emit(msg, payload);
this.eventEmitter.emit('*', payload); this.eventEmitter.emit(Session.ANY_PDU_EVENT, payload);
} }
}) })
this.connectingPromise.resolve(); this.connectingPromise.resolve();
@@ -249,8 +252,7 @@ class Session {
source_addr: source, source_addr: source,
destination_addr: destination, destination_addr: destination,
short_message: message short_message: message
}, function(pdu) { }, pdu => {
console.log(pdu);
}); });
this.session.close(); this.session.close();
} }
@@ -284,6 +286,7 @@ class Session {
} }
class SessionManager { class SessionManager {
// TODO: Also enable session deletion (not just disconnection)
sessionIdCounter = 0; sessionIdCounter = 0;
logger = new Logger("SessionManager"); logger = new Logger("SessionManager");
@@ -303,10 +306,6 @@ class SessionManager {
this.sessions.push(session); this.sessions.push(session);
} }
getSessions() {
return this.sessions;
}
getSession(id) { getSession(id) {
return this.sessions.find((session) => { return this.sessions.find((session) => {
return session.id == id; return session.id == id;
@@ -437,6 +436,10 @@ class WSServer {
addClient(ws, sessionId) { addClient(ws, sessionId) {
if (!this.clients[sessionId]) { if (!this.clients[sessionId]) {
this.clients[sessionId] = []; this.clients[sessionId] = [];
let session = sessionManager.getSession(sessionId);
if (session) {
session.on(Session.STATUS_CHANGED_EVENT, this.onSessionChange.bind(this, sessionId));
}
} }
this.logger.log1(`Added client to session ID: ${sessionId}`); this.logger.log1(`Added client to session ID: ${sessionId}`);
this.clients[sessionId].push(ws); this.clients[sessionId].push(ws);
@@ -465,15 +468,20 @@ class WSServer {
delete this.clients[sessionId][index]; delete this.clients[sessionId][index];
} }
} }
this.cleanClients();
} }
cleanClients() { onSessionChange(sessionId, session) {
for (let sessionId in this.clients) { // TODO: Also maybe create a broadcast for any pdu
this.clients[sessionId] = this.clients[sessionId].filter(Boolean); // To do this add ssomething to the client message maybe like sessionId:listenToPdu?
if (this.clients[sessionId].length === 0) { // So something like 0:1 or 0:true
delete this.clients[sessionId]; // Then send any pdu updates to all clients with listen to true
} this.logger.log1(`Session with ID ${sessionId} changed`);
if (this.clients[sessionId]) {
this.logger.log1(`Broadcasting session with ID ${sessionId} to ${this.clients[sessionId].length} clients`)
let value = session.serialize();
this.clients[sessionId].forEach(client => {
client.send(JSON.stringify(value));
});
} }
} }
} }
@@ -486,22 +494,5 @@ 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.on('statusChanged', (status) => {
logger.log1(`NEW STATUS! ${status}`);
});
// session.on('*', pdu => {
// logger.log1(pdu);
// });
// session.connect().then(() => {
// session.bind().catch(() => {
// logger.log1("AAA");
// })
// .then(() => {
// logger.log1("OK");
// session.send("123", "456", "test");
// sleep(600 * 1000);
// });
// });
new WSServer(); new WSServer();
new HTTPServer(); new HTTPServer();

View File

@@ -5,7 +5,7 @@ const WS_SERVER_PORT = process.env.WS_SERVER_PORT || 8191;
const ws = new WebSocket(`ws://localhost:${WS_SERVER_PORT}`); const ws = new WebSocket(`ws://localhost:${WS_SERVER_PORT}`);
ws.on('open', () => { ws.on('open', () => {
console.log('WebSocket connection established'); console.log('WebSocket connection established');
ws.send(3); ws.send(0);
}); });
ws.on('message', (data) => { ws.on('message', (data) => {
console.log(data); console.log(data);