Skip to content

Commit

Permalink
closeConnection logic adding
Browse files Browse the repository at this point in the history
  • Loading branch information
willwade committed Oct 30, 2024
1 parent 2a8031b commit 0d14d66
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
10 changes: 7 additions & 3 deletions nodejs/sender-monitor/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,13 @@ app.on("before-quit", async () => {
}
}

if (webrtc.isConnected()) {
webrtc.closeConnection();
logMessage("WebRTC connection closed.");
if (webrtc.connected) { // Replace `connected` with the correct property or method if different
try {
await webrtc.closeConnection(); // Make sure closeConnection is a Promise, or remove `await`
logMessage("WebRTC connection closed.");
} catch (error) {
logMessage(`Failed to close WebRTC connection: ${error.message}`);
}
}

const tempFilePath = path.join(app.getPath("temp"), "ocr-capture.png");
Expand Down
23 changes: 23 additions & 0 deletions nodejs/sender-monitor/webrtc.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,29 @@ class WebRTCConnection extends EventEmitter {
}
});
}

closeConnection() {
Object.values(this.peerConnections).forEach((peerConnection) => {
peerConnection.close();
});
Object.values(this.dataChannels).forEach((dataChannel) => {
if (dataChannel.readyState !== "closed") {
dataChannel.close();
}
});

// Clear the connection objects
this.peerConnections = {};
this.dataChannels = {};

// Disconnect the WebSocket connection
if (this.socket.connected) {
this.socket.disconnect();
}

this.emit("disconnected");
console.log("WebRTC connection closed.");
}
}

module.exports = new WebRTCConnection();

0 comments on commit 0d14d66

Please sign in to comment.