add background auth and login
This commit is contained in:
parent
29f6b759f8
commit
c9f8ea6337
3 changed files with 46 additions and 67 deletions
|
|
@ -22,7 +22,7 @@ const updateRecorder = async (Event: any, record: boolean): void => {
|
|||
}
|
||||
|
||||
|
||||
let ia = new portAudio.AudioIO({
|
||||
const ia = new portAudio.AudioIO({
|
||||
inOptions: {
|
||||
channelCount: 1,
|
||||
sampleFormat: 16,
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
const authUser = (event: any, arg: any) => {
|
||||
export const authUser = (event: any, arg: any) => {
|
||||
let reply;
|
||||
reply = {
|
||||
id: "2",
|
||||
|
|
@ -7,16 +7,14 @@ const authUser = (event: any, arg: any) => {
|
|||
firstName: "Enrique",
|
||||
lastName: "Hernandez",
|
||||
access_token: "test-token"
|
||||
}
|
||||
};
|
||||
if (arg === ['undefined']) {
|
||||
reply = new Error('AUTH-FAILED');
|
||||
}
|
||||
event.reply('auth-user-reply', reply)
|
||||
event.reply('auth-user-reply', reply);
|
||||
}
|
||||
|
||||
ipcMain.on('auth-user', authUser);
|
||||
|
||||
const authLogin = (event: any, arg: any) => {
|
||||
console.log(arg)
|
||||
event.reply('auth-login-reply', true)
|
||||
export const authLogin = (event: any, arg: any) => {
|
||||
console.log('testing login', arg);
|
||||
event.reply('auth-login-reply', true);
|
||||
}
|
||||
|
|
@ -2,6 +2,7 @@ import { createWindow } from './createWindow';
|
|||
import { ipcMain } from "electron";
|
||||
import WebSocket from 'ws';
|
||||
import { windowEmitter } from './windowEmitter';
|
||||
import { authUser, authLogin } from './auth';
|
||||
const portAudio = require('naudiodon');
|
||||
|
||||
/*
|
||||
|
|
@ -9,29 +10,9 @@ const portAudio = require('naudiodon');
|
|||
*/
|
||||
export function main(): void {
|
||||
|
||||
let audioInput: string[] = [];
|
||||
|
||||
const authUser = (event: any, arg: any) => {
|
||||
let reply;
|
||||
reply = {
|
||||
id: "2",
|
||||
email: "hernandeze@xavier.edu",
|
||||
firstName: "Enrique",
|
||||
lastName: "Hernandez",
|
||||
access_token: "test-token"
|
||||
}
|
||||
if (arg === ['undefined']) {
|
||||
reply = new Error('AUTH-FAILED');
|
||||
}
|
||||
event.reply('auth-user-reply', reply)
|
||||
}
|
||||
// mount auth-user listener
|
||||
ipcMain.on('auth-user', authUser);
|
||||
|
||||
const authLogin = (event: any, arg: any) => {
|
||||
console.log(arg)
|
||||
event.reply('auth-login-reply', true)
|
||||
}
|
||||
|
||||
// create main window.
|
||||
const win = createWindow({ width: 350, height: 525, resizable: false });
|
||||
|
||||
|
|
@ -59,27 +40,9 @@ export function main(): void {
|
|||
});
|
||||
}
|
||||
|
||||
const updateRecorder = async (Event: any, record: boolean): void => {
|
||||
if (record) {
|
||||
ia.resume();
|
||||
} else {
|
||||
ia.pause();
|
||||
const audio = processAudio(audioInput);
|
||||
sendAudio(audio);
|
||||
audioInput = [];
|
||||
}
|
||||
}
|
||||
|
||||
function processAudio(audioInput: Array<string>): Buffer {
|
||||
let audio = '';
|
||||
audioInput.forEach(chunk => audio += chunk);
|
||||
return Buffer.from(audio, "base64");
|
||||
}
|
||||
|
||||
const windowMount = (): void => {
|
||||
windowEmitter.emit('window-active', true);
|
||||
// ipc event handlers
|
||||
ipcMain.on('update-recorder', updateRecorder);
|
||||
ipcMain.on('send-message', sendMessage);
|
||||
ipcMain.on('auth-login', authLogin);
|
||||
}
|
||||
|
|
@ -87,7 +50,6 @@ export function main(): void {
|
|||
const windowDismount = (): void => {
|
||||
windowEmitter.emit('window-active', false);
|
||||
// TODO: Gracefully close socket connection
|
||||
ipcMain.removeAllListeners('update-recorder');
|
||||
ipcMain.removeAllListeners('send-message');
|
||||
ipcMain.removeAllListeners('auth-login');
|
||||
ipcMain.removeAllListeners('auth-user');
|
||||
|
|
@ -101,22 +63,41 @@ export function main(): void {
|
|||
win.webContents.on('did-finish-load', windowMount);
|
||||
win.on("closed", windowDismount);
|
||||
|
||||
let ia = new portAudio.AudioIO({
|
||||
inOptions: {
|
||||
channelCount: 1,
|
||||
sampleFormat: 16,
|
||||
sampleRate: 16000,
|
||||
deviceId: -1,
|
||||
closeOnError: false,
|
||||
}
|
||||
});
|
||||
ia.setEncoding('base64');
|
||||
ia.start();
|
||||
ia.pause();
|
||||
ia.on('error', (e: Error) => {
|
||||
console.log('Error recording audio', + e);
|
||||
});
|
||||
ia.on('data', (chunk: string) => {
|
||||
audioInput.push(chunk);
|
||||
});
|
||||
// TODO: FIX recorder restart bug
|
||||
// let audioInput: string[] = [];
|
||||
// const updateRecorder = async (Event: any, record: boolean): void => {
|
||||
// if (record) {
|
||||
// ia.resume();
|
||||
// } else {
|
||||
// ia.pause();
|
||||
// const audio = processAudio(audioInput);
|
||||
// sendAudio(audio);
|
||||
// audioInput = [];
|
||||
// }
|
||||
// }
|
||||
|
||||
// function processAudio(audioInput: Array<string>): Buffer {
|
||||
// let audio = '';
|
||||
// audioInput.forEach(chunk => audio += chunk);
|
||||
// return Buffer.from(audio, "base64");
|
||||
// }
|
||||
|
||||
// const ia = new portAudio.AudioIO({
|
||||
// inOptions: {
|
||||
// channelCount: 1,
|
||||
// sampleFormat: 16,
|
||||
// sampleRate: 16000,
|
||||
// deviceId: -1,
|
||||
// closeOnError: false,
|
||||
// }
|
||||
// });
|
||||
// ia.setEncoding('base64');
|
||||
// ia.start();
|
||||
// ia.pause();
|
||||
// ia.on('error', (e: Error) => {
|
||||
// console.log('Error recording audio', + e);
|
||||
// });
|
||||
// ia.on('data', (chunk: string) => {
|
||||
// audioInput.push(chunk);
|
||||
// });
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue