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

27
main.js
View File

@ -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,6 +75,7 @@ function muteDelay() {
// Globally mutes the Mic
function muteMic() {
if (isConnected === true) {
return new Promise((resolve) => {
if (isTalking === false) {
muteDelay().then(val => {
@ -88,9 +90,11 @@ function muteMic() {
}
})
}
}
// Globally unmutes the Mic
function unmuteMic() {
if (isConnected === true) {
return new Promise((resolve, reject) => {
console.log("Talking")
isTalking = true
@ -100,16 +104,10 @@ function unmuteMic() {
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 => {
@ -124,3 +122,16 @@ ioHook.on('mouseup', event => {
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 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()