Skip to content

Commit

Permalink
add persistentId setup.. not loving it
Browse files Browse the repository at this point in the history
  • Loading branch information
willwade committed Nov 8, 2024
1 parent 7a3c7ae commit 90dfb7f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
7 changes: 5 additions & 2 deletions nodejs/sender-monitor/main.js
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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.");

Expand Down
29 changes: 19 additions & 10 deletions nodejs/sender-monitor/webrtc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,46 @@ 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();
const word3 = faker.word.noun();
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", () => {
Expand Down Expand Up @@ -146,4 +155,4 @@ class WebRTCConnection extends EventEmitter {
}
}

module.exports = new WebRTCConnection();
module.exports = WebRTCConnection;

0 comments on commit 90dfb7f

Please sign in to comment.