Implement websocket notifications for center status, mode and few other changes
This commit is contained in:
156
main.js
156
main.js
@@ -1173,6 +1173,7 @@ class WSServer {
|
|||||||
// TODO: Implement center adding and removing...
|
// TODO: Implement center adding and removing...
|
||||||
// TODO: This will probably have to be reworked a little to accommodate centers.
|
// TODO: This will probably have to be reworked a little to accommodate centers.
|
||||||
clients = {};
|
clients = {};
|
||||||
|
unknownClients = [];
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.server = new WebSocket.Server({port: WS_SERVER_PORT});
|
this.server = new WebSocket.Server({port: WS_SERVER_PORT});
|
||||||
@@ -1183,33 +1184,54 @@ class WSServer {
|
|||||||
|
|
||||||
onConnection(ws) {
|
onConnection(ws) {
|
||||||
this.logger.log1("New connection");
|
this.logger.log1("New connection");
|
||||||
this.addClient(ws, -1);
|
this.unknownClients.push(ws);
|
||||||
ws.on('message', this.onMessage.bind(this, ws));
|
ws.on('message', this.onMessage.bind(this, ws));
|
||||||
ws.on('close', this.onClose.bind(this, ws));
|
ws.on('close', this.onClose.bind(this, ws));
|
||||||
}
|
}
|
||||||
|
|
||||||
addClient(ws, sessionId) {
|
addClient(ws, type, sessionId) {
|
||||||
if (!this.clients[sessionId]) {
|
if (!this.clients[type]) {
|
||||||
this.clients[sessionId] = [];
|
this.clients[type] = {};
|
||||||
|
}
|
||||||
|
if (!this.clients[type][sessionId]) {
|
||||||
|
this.clients[type][sessionId] = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (type === "client") {
|
||||||
let session = clientSessionManager.getSession(sessionId);
|
let session = clientSessionManager.getSession(sessionId);
|
||||||
if (session) {
|
if (!!session) {
|
||||||
session.on(ClientSession.STATUS_CHANGED_EVENT, this.onSessionChange.bind(this, sessionId));
|
session.on(ClientSession.STATUS_CHANGED_EVENT, this.onClientSessionStatusChange.bind(this, sessionId));
|
||||||
session.on(ClientSession.ANY_PDU_EVENT, this.pduEvent.bind(this, sessionId));
|
session.on(ClientSession.ANY_PDU_EVENT, this.onClientSessionPdu.bind(this, sessionId));
|
||||||
session.on(ClientSession.MESSAGE_SEND_COUNTER_UPDATE_EVENT, this.onMessageSendCounterUpdate.bind(this, sessionId));
|
session.on(ClientSession.MESSAGE_SEND_COUNTER_UPDATE_EVENT, this.onClientMessageCounterUpdate.bind(this, sessionId));
|
||||||
|
}
|
||||||
|
} else if (type === "center") {
|
||||||
|
let session = centerSessionManager.getSession(sessionId);
|
||||||
|
if (!!session) {
|
||||||
|
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.MODE_CHANGED_EVENT, this.onCenterModeChanged.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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.logger.log1(`Added client to session ID: ${sessionId}`);
|
|
||||||
this.clients[sessionId].push(ws);
|
this.clients[type][sessionId].push(ws);
|
||||||
this.logger.log1(`Now active ${this.clients[sessionId].length} clients in session ID: ${sessionId}`);
|
this.logger.log1(`Now active ${this.clients[type][sessionId].length} clients in session ID: ${sessionId} of type ${type}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
onMessage(ws, message) {
|
onMessage(ws, message) {
|
||||||
this.logger.log1("New message");
|
this.logger.log1("New message");
|
||||||
let sessionId = String(message);
|
message = String(message);
|
||||||
this.logger.log1(`Moving client to session ID: ${sessionId}`);
|
let data = message.split(":");
|
||||||
this.removeClient(ws);
|
let type = data[0];
|
||||||
this.addClient(ws, sessionId);
|
let sessionId = data[1];
|
||||||
this.logger.log1(`Now active ${this.clients[sessionId].length} clients in session ID: ${sessionId}`);
|
|
||||||
|
this.logger.log1(`Moving client to session ID: ${sessionId} of type ${type}`);
|
||||||
|
delete this.unknownClients[ws];
|
||||||
|
this.unknownClients = this.unknownClients.filter(Boolean);
|
||||||
|
|
||||||
|
this.addClient(ws, type, sessionId);
|
||||||
|
this.logger.log1(`Now active ${this.clients[type][sessionId].length} clients in session ID: ${sessionId} of type ${type}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
onClose(ws) {
|
onClose(ws) {
|
||||||
@@ -1219,6 +1241,7 @@ class WSServer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
removeClient(ws) {
|
removeClient(ws) {
|
||||||
|
// TODO: Fix this
|
||||||
for (let sessionId in this.clients) {
|
for (let sessionId in this.clients) {
|
||||||
let index = this.clients[sessionId].indexOf(ws);
|
let index = this.clients[sessionId].indexOf(ws);
|
||||||
if (index > -1) {
|
if (index > -1) {
|
||||||
@@ -1231,14 +1254,15 @@ class WSServer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onSessionChange(sessionId, newStatus) {
|
onClientSessionStatusChange(sessionId, newStatus) {
|
||||||
this.logger.log1(`Session with ID ${sessionId} changed`);
|
this.logger.log1(`Session with ID ${sessionId} changed`);
|
||||||
let payload = {
|
let payload = {
|
||||||
|
objectType: "client",
|
||||||
type: 'status',
|
type: 'status',
|
||||||
sessionId: sessionId,
|
sessionId: sessionId,
|
||||||
value: newStatus
|
value: newStatus
|
||||||
}
|
}
|
||||||
let clients = this.clients[sessionId];
|
let clients = this.clients["client"][sessionId];
|
||||||
if (!!clients) {
|
if (!!clients) {
|
||||||
this.logger.log1(`Broadcasting session with ID ${sessionId} to ${clients.length} clients`);
|
this.logger.log1(`Broadcasting session with ID ${sessionId} to ${clients.length} clients`);
|
||||||
clients.forEach(client => {
|
clients.forEach(client => {
|
||||||
@@ -1247,11 +1271,12 @@ class WSServer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pduEvent(sessionId, pdu) {
|
onClientSessionPdu(sessionId, pdu) {
|
||||||
let clients = this.clients[sessionId];
|
let clients = this.clients["client"][sessionId];
|
||||||
if (!!clients) {
|
if (!!clients) {
|
||||||
this.logger.log2(`Session with ID ${sessionId} fired PDU`);
|
this.logger.log2(`Session with ID ${sessionId} fired PDU`);
|
||||||
let payload = {
|
let payload = {
|
||||||
|
objectType: "client",
|
||||||
type: 'pdu',
|
type: 'pdu',
|
||||||
sessionId: sessionId,
|
sessionId: sessionId,
|
||||||
value: pdu
|
value: pdu
|
||||||
@@ -1263,14 +1288,101 @@ class WSServer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onMessageSendCounterUpdate(sessionId, counter) {
|
onClientMessageCounterUpdate(sessionId, counter) {
|
||||||
this.logger.log2(`Session with ID ${sessionId} updating message send counter`);
|
this.logger.log2(`Session with ID ${sessionId} updating message send counter`);
|
||||||
let payload = {
|
let payload = {
|
||||||
|
objectType: "client",
|
||||||
type: 'counterUpdate',
|
type: 'counterUpdate',
|
||||||
sessionId: sessionId,
|
sessionId: sessionId,
|
||||||
value: counter
|
value: counter
|
||||||
}
|
}
|
||||||
let clients = this.clients[sessionId];
|
let clients = this.clients["client"][sessionId];
|
||||||
|
if (!!clients) {
|
||||||
|
this.logger.log2(`Broadcasting session with ID ${sessionId} to ${clients.length} clients`);
|
||||||
|
clients.forEach(client => {
|
||||||
|
client.send(JSON.stringify(payload));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
onCenterStatusChange(sessionId, newStatus) {
|
||||||
|
this.logger.log1(`Session with ID ${sessionId} changed`);
|
||||||
|
let payload = {
|
||||||
|
objectType: "center",
|
||||||
|
type: 'status',
|
||||||
|
sessionId: sessionId,
|
||||||
|
value: newStatus
|
||||||
|
}
|
||||||
|
let clients = this.clients["center"][sessionId];
|
||||||
|
if (!!clients) {
|
||||||
|
this.logger.log1(`Broadcasting session with ID ${sessionId} to ${clients.length} clients`);
|
||||||
|
clients.forEach(client => {
|
||||||
|
client.send(JSON.stringify(payload));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
onCenterServerPdu(sessionId, pdu) {
|
||||||
|
let clients = this.clients["center"][sessionId];
|
||||||
|
if (!!clients) {
|
||||||
|
this.logger.log2(`Session with ID ${sessionId} fired PDU`);
|
||||||
|
let payload = {
|
||||||
|
objectType: "center",
|
||||||
|
type: 'pdu',
|
||||||
|
sessionId: sessionId,
|
||||||
|
value: pdu
|
||||||
|
}
|
||||||
|
this.logger.log2(`Broadcasting session with ID ${sessionId} to ${clients.length} clients`);
|
||||||
|
clients.forEach(client => {
|
||||||
|
client.send(JSON.stringify(payload));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
onCenterModeChanged(sessionId, newMode) {
|
||||||
|
this.logger.log1(`Session with ID ${sessionId} changed`);
|
||||||
|
let payload = {
|
||||||
|
objectType: "center",
|
||||||
|
type: 'mode',
|
||||||
|
sessionId: sessionId,
|
||||||
|
value: newMode,
|
||||||
|
text: CenterMode[newMode]
|
||||||
|
}
|
||||||
|
let clients = this.clients["center"][sessionId];
|
||||||
|
if (!!clients) {
|
||||||
|
this.logger.log1(`Broadcasting session with ID ${sessionId} to ${clients.length} clients`);
|
||||||
|
clients.forEach(client => {
|
||||||
|
client.send(JSON.stringify(payload));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
onCenterSessionsChanged(sessionId, newSession) {
|
||||||
|
this.logger.log1(`Session with ID ${sessionId} changed`);
|
||||||
|
let payload = {
|
||||||
|
objectType: "center",
|
||||||
|
type: 'sessions',
|
||||||
|
sessionId: sessionId,
|
||||||
|
value: newSession
|
||||||
|
}
|
||||||
|
let clients = this.clients["center"][sessionId];
|
||||||
|
if (!!clients) {
|
||||||
|
this.logger.log1(`Broadcasting session with ID ${sessionId} to ${clients.length} clients`);
|
||||||
|
clients.forEach(client => {
|
||||||
|
client.send(JSON.stringify(payload));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
onCenterMessageCounterUpdate(sessionId, counter) {
|
||||||
|
this.logger.log2(`Session with ID ${sessionId} updating message send counter`);
|
||||||
|
let payload = {
|
||||||
|
objectType: "center",
|
||||||
|
type: 'counterUpdate',
|
||||||
|
sessionId: sessionId,
|
||||||
|
value: counter
|
||||||
|
}
|
||||||
|
let clients = this.clients["center"][sessionId];
|
||||||
if (!!clients) {
|
if (!!clients) {
|
||||||
this.logger.log2(`Broadcasting session with ID ${sessionId} to ${clients.length} clients`);
|
this.logger.log2(`Broadcasting session with ID ${sessionId} to ${clients.length} clients`);
|
||||||
clients.forEach(client => {
|
clients.forEach(client => {
|
||||||
|
@@ -3,10 +3,18 @@ const WebSocket = require('ws');
|
|||||||
const WS_SERVER_PORT = process.env.WS_SERVER_PORT || 8191;
|
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}`);
|
||||||
|
const ws2 = 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(0);
|
ws.send("client:0");
|
||||||
});
|
});
|
||||||
ws.on('message', (data) => {
|
ws.on('message', (data) => {
|
||||||
console.log(String(data));
|
console.log(String(data));
|
||||||
});
|
});
|
||||||
|
ws2.on('open', () => {
|
||||||
|
console.log('WebSocket connection established');
|
||||||
|
ws.send("center:0");
|
||||||
|
});
|
||||||
|
ws2.on('message', (data) => {
|
||||||
|
console.log(String(data));
|
||||||
|
});
|
Reference in New Issue
Block a user