]> Repos - mime-chat/commitdiff
add background auth and login
authorriqo <hernandeze2@xavier.edu>
Mon, 8 Feb 2021 15:50:35 +0000 (09:50 -0600)
committerriqo <hernandeze2@xavier.edu>
Mon, 8 Feb 2021 15:50:35 +0000 (09:50 -0600)
src/background/audio.ts
src/background/auth.ts [moved from src/background/user.ts with 53% similarity]
src/background/run.ts

index 3ce3d89784474c1eb881ac0c2fdeb51db6579165..7a30f325e5e56abf302d4255a135e54667b55b5b 100644 (file)
@@ -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,
similarity index 53%
rename from src/background/user.ts
rename to src/background/auth.ts
index 12582573501aaa933d652b61b34ac01f8ec8a0bf..81e086f9fde67a465e832092ccf274f6cad8a315 100644 (file)
@@ -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);
 }
index 49ccacaba1e3588a8ed112767d2d9b5a3fb44bf7..f64e1818349348b479a23e46133a69340a9b7360 100644 (file)
@@ -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);
+    // });
 }