diff --git a/main.js b/main.js index 160661b..8942efb 100644 --- a/main.js +++ b/main.js @@ -1,5 +1,5 @@ // Modules to control application life and create native browser window -const {app, BrowserWindow, ipcRenderer} = require('electron') +const {app, BrowserWindow, ipcMain} = require('electron') const path = require('path') // Keep a global reference of the window object, if you don't, the window will @@ -62,6 +62,7 @@ const win = require('win-audio') const microphone = win.mic let isTalking = false +let isConnected = false // Resolves the promise after 2 seconds function muteDelay() { @@ -74,42 +75,39 @@ function muteDelay() { // Globally mutes the Mic function muteMic() { - return new Promise((resolve) => { - if (isTalking === false) { - muteDelay().then(val => { - if (isTalking === false) { - microphone.mute() // Mute mic - console.log("Muted") - mainWindow.webContents.send('ping', 'mic-closed') - mainWindow.setTitle("MUTED") - return resolve(true) - } - }) - } - }) + if (isConnected === true) { + return new Promise((resolve) => { + if (isTalking === false) { + muteDelay().then(val => { + if (isTalking === false) { + microphone.mute() // Mute mic + console.log("Muted") + mainWindow.webContents.send('ping', 'mic-closed') + mainWindow.setTitle("MUTED") + return resolve(true) + } + }) + } + }) + } } // Globally unmutes the Mic function unmuteMic() { - return new Promise((resolve, reject) => { - console.log("Talking") - isTalking = true - mainWindow.webContents.send('ping', 'mic-open') - mainWindow.setTitle("MIC OPEN") - microphone.unmute(); // Unmute mic - return resolve(true) - }) + if (isConnected === true) { + return new Promise((resolve, reject) => { + console.log("Talking") + isTalking = true + mainWindow.webContents.send('ping', 'mic-open') + mainWindow.setTitle("MIC OPEN") + microphone.unmute(); // Unmute mic + return resolve(true) + }) + } } app.on('ready', event => { ioHook.start(); - - console.log("Init Finished") - - console.log("Muted") - microphone.mute(); - mainWindow.webContents.send('ping', 'mic-closed') - mainWindow.setTitle("MUTED") }) ioHook.on('mousedown', event => { @@ -123,4 +121,17 @@ ioHook.on('mouseup', event => { isTalking = false muteMic() } -}) \ No newline at end of file +}) + +ipcMain.on('asynchronous-message', (event, arg) => { + if (arg === 'connected') { + isConnected = true + } + + if (arg === 'disconnected') { + isConnected = false + isTalking = false + microphone.unmute(); // Unmute mic + } + console.log(arg) +}) diff --git a/renderer.js b/renderer.js index 3c1adc6..d6a64f6 100644 --- a/renderer.js +++ b/renderer.js @@ -1,20 +1,25 @@ const unMuteSound = new Audio('./assets/unmute.mp3') const muteSound = new Audio('./assets/mute.mp3') -const ipc = require('electron').ipcRenderer; + +const { ipcRenderer } = require('electron') onload = () => { const webview = document.querySelector('webview') webview.openDevTools() webview.addEventListener('console-message', (e) => { - console.log('D: ', e.message) + if (e.message === "Constructed RTCPeerConnection") { + console.log("Connected to server") + ipcRenderer.send('asynchronous-message', 'connected') + } if (e.message === "Close RTCPeerConnection") { console.log("Disconnected from server") + ipcRenderer.send('asynchronous-message', 'disconnected') } }) } -require('electron').ipcRenderer.on('ping', (event, message) => { +ipcRenderer.on('ping', (event, message) => { if (message === 'mic-open'){ console.log("mic is open") unMuteSound.play()