Skip to content

Commit

Permalink
Merge pull request #3 from AceCentre/persistentId-changes
Browse files Browse the repository at this point in the history
Persistent id changes
  • Loading branch information
willwade authored Nov 8, 2024
2 parents d3dbfe4 + 90dfb7f commit c2181d5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 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
26 changes: 22 additions & 4 deletions nodejs/sender-monitor/webrtc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,50 @@ 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 = this.generateSessionId();
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() {
if (this.config && this.config.sessionId) {
this.sessionPersistent = true;
return this.config.sessionId;
} else {
this.sessionPersistent = false;
return this.generateSessionId();
}
}

startSession() {
console.log(`Session ID: ${this.sessionId}`);
return this.sessionId;
}

setupSocketListeners() {
this.getSessionId()
this.socket.on("connect", () => {
this.socket.emit("joinSession", this.sessionId);
this.socket.emit("joinSession", this.sessionId, this.sessionPersistent);
this.emit("connected"); // Emit connected event
});

Expand Down Expand Up @@ -137,4 +155,4 @@ class WebRTCConnection extends EventEmitter {
}
}

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

0 comments on commit c2181d5

Please sign in to comment.