connect/disconnect test

This commit is contained in:
khlam 2019-10-14 12:59:30 -07:00
parent 08bbc24504
commit e72ed16b63
2 changed files with 49 additions and 33 deletions

69
main.js
View File

@ -1,5 +1,5 @@
// Modules to control application life and create native browser window // 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') const path = require('path')
// Keep a global reference of the window object, if you don't, the window will // 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 const microphone = win.mic
let isTalking = false let isTalking = false
let isConnected = false
// Resolves the promise after 2 seconds // Resolves the promise after 2 seconds
function muteDelay() { function muteDelay() {
@ -74,42 +75,39 @@ function muteDelay() {
// Globally mutes the Mic // Globally mutes the Mic
function muteMic() { function muteMic() {
return new Promise((resolve) => { if (isConnected === true) {
if (isTalking === false) { return new Promise((resolve) => {
muteDelay().then(val => { if (isTalking === false) {
if (isTalking === false) { muteDelay().then(val => {
microphone.mute() // Mute mic if (isTalking === false) {
console.log("Muted") microphone.mute() // Mute mic
mainWindow.webContents.send('ping', 'mic-closed') console.log("Muted")
mainWindow.setTitle("MUTED") mainWindow.webContents.send('ping', 'mic-closed')
return resolve(true) mainWindow.setTitle("MUTED")
} return resolve(true)
}) }
} })
}) }
})
}
} }
// Globally unmutes the Mic // Globally unmutes the Mic
function unmuteMic() { function unmuteMic() {
return new Promise((resolve, reject) => { if (isConnected === true) {
console.log("Talking") return new Promise((resolve, reject) => {
isTalking = true console.log("Talking")
mainWindow.webContents.send('ping', 'mic-open') isTalking = true
mainWindow.setTitle("MIC OPEN") mainWindow.webContents.send('ping', 'mic-open')
microphone.unmute(); // Unmute mic mainWindow.setTitle("MIC OPEN")
return resolve(true) microphone.unmute(); // Unmute mic
}) return resolve(true)
})
}
} }
app.on('ready', event => { app.on('ready', event => {
ioHook.start(); ioHook.start();
console.log("Init Finished")
console.log("Muted")
microphone.mute();
mainWindow.webContents.send('ping', 'mic-closed')
mainWindow.setTitle("MUTED")
}) })
ioHook.on('mousedown', event => { ioHook.on('mousedown', event => {
@ -124,3 +122,16 @@ ioHook.on('mouseup', event => {
muteMic() muteMic()
} }
}) })
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)
})

View File

@ -1,20 +1,25 @@
const unMuteSound = new Audio('./assets/unmute.mp3') const unMuteSound = new Audio('./assets/unmute.mp3')
const muteSound = new Audio('./assets/mute.mp3') const muteSound = new Audio('./assets/mute.mp3')
const ipc = require('electron').ipcRenderer;
const { ipcRenderer } = require('electron')
onload = () => { onload = () => {
const webview = document.querySelector('webview') const webview = document.querySelector('webview')
webview.openDevTools() webview.openDevTools()
webview.addEventListener('console-message', (e) => { 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") { if (e.message === "Close RTCPeerConnection") {
console.log("Disconnected from server") 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'){ if (message === 'mic-open'){
console.log("mic is open") console.log("mic is open")
unMuteSound.play() unMuteSound.play()