From 90dfb7f7d98b5556a79d444b66e1c904dfade49b Mon Sep 17 00:00:00 2001 From: will wade Date: Fri, 8 Nov 2024 00:50:30 +0000 Subject: [PATCH] add persistentId setup.. not loving it --- nodejs/sender-monitor/main.js | 7 +++++-- nodejs/sender-monitor/webrtc.js | 29 +++++++++++++++++++---------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/nodejs/sender-monitor/main.js b/nodejs/sender-monitor/main.js index b3b2d64..e578390 100644 --- a/nodejs/sender-monitor/main.js +++ b/nodejs/sender-monitor/main.js @@ -1,7 +1,7 @@ const { app, Tray, Menu, shell, clipboard, BrowserWindow, ipcMain, screen} = require("electron"); const path = require("path"); const fs = require("fs"); -const webrtc = require("./webrtc"); +const WebRTCConnection = require("./webrtc"); const QRCode = require("qrcode"); const { Monitor } = require("node-screenshots"); const { performOCR } = require("./ocr"); @@ -32,7 +32,7 @@ if (app.isPackaged && !fs.existsSync(configFilePath)) { } let config = JSON.parse(fs.readFileSync(configFilePath, "utf-8")); - +const webrtc = new WebRTCConnection(config); ipcMain.on("close-qr-window", () => { if (qrWindow) { @@ -121,6 +121,9 @@ async function createQRWindow(qrDataUrl) { function reloadConfig() { try { config = JSON.parse(fs.readFileSync(configFilePath, "utf-8")); + if (config.sessionId !== webrtc.sessionId) { + webrtc.updateConfig(config); + } console.log("Configuration reloaded."); logMessage("Configuration reloaded from config.json."); diff --git a/nodejs/sender-monitor/webrtc.js b/nodejs/sender-monitor/webrtc.js index 90de31f..00f6ee3 100644 --- a/nodejs/sender-monitor/webrtc.js +++ b/nodejs/sender-monitor/webrtc.js @@ -5,18 +5,24 @@ const faker = require("@faker-js/faker").faker; const iceServers = require("./iceServers"); const WEBSOCKET_URL = "wss://owd.acecentre.net"; +//const WEBSOCKET_URL = "ws://localhost:3000"; class WebRTCConnection extends EventEmitter { - constructor() { + constructor(config) { super(); this.peerConnections = {}; this.dataChannels = {}; - this.sessionId = 'not-set-sessionid'; - this.sessionPersistent = false; + this.config; + this.sessionId = this.getSessionId(); this.socket = io(WEBSOCKET_URL, { transports: ["websocket"], withCredentials: true }); this.setupSocketListeners(); } + updateConfig(newConfig) { + this.config = newConfig; + this.sessionId = this.getSessionId(); + } + generateSessionId() { const word1 = faker.word.adjective(); const word2 = faker.word.adjective(); @@ -24,18 +30,21 @@ class WebRTCConnection extends EventEmitter { return `${word1}-${word2}-${word3}`; } - getSessionId() { - // Check if sessionId is already stored in config - if (config.sessionId) { - this.sessionId = config.sessionId; + getSessionId() { + if (this.config && this.config.sessionId) { this.sessionPersistent = true; + return this.config.sessionId; } else { - this.sessionId = this.generateSessionId(); - config.sessionId = this.sessionId; this.sessionPersistent = false; + return this.generateSessionId(); } } + startSession() { + console.log(`Session ID: ${this.sessionId}`); + return this.sessionId; + } + setupSocketListeners() { this.getSessionId() this.socket.on("connect", () => { @@ -146,4 +155,4 @@ class WebRTCConnection extends EventEmitter { } } -module.exports = new WebRTCConnection(); +module.exports = WebRTCConnection;