From: Andrew Gundersen Date: Thu, 18 Mar 2021 16:56:47 +0000 (-0500) Subject: updating X-Git-Tag: v0.9~24 X-Git-Url: https://andrewgundersen.net/repos?a=commitdiff_plain;h=3849a3eaa841beadf23a8a7c93e825c69a6028bd;p=mime-chat updating --- diff --git a/src/background/session.ts b/src/background/session.ts index 92609db..a67a452 100644 --- a/src/background/session.ts +++ b/src/background/session.ts @@ -4,7 +4,15 @@ import WebSocket from 'ws'; import { backgroundMitt } from './emitter'; import { ipcMain } from "electron"; import { play } from './audio'; -import { UserCreds, ClientMessage, RenderMessage, Annotation } from "@/types/message/index"; + +import { + AuthMessage, + Profile, + Annotation, + StandardMessage, + ClientMessage +} from "@/types/message/index"; + import { clientMessage, renderMessage } from '@/modules/message'; const ip = 'ws://127.0.0.1'; @@ -14,14 +22,16 @@ let socket: WebSocket; let success = false; let auth = false; -const onAuthSession = async (e: any, payload: string | UserCreds | null): Promise => { +const onAuthSession = async (e: any, token: string) => { + return new Promise((resolve, reject) => { - console.log('authenticating...'); + console.log('Authenticating...'); + // Handle auth response from server. backgroundMitt.once('auth-res', (res: string) => { - if (res === 'locked') { + if (res == "locked") { reject(res); } else { console.log('Success!'); @@ -31,50 +41,75 @@ const onAuthSession = async (e: any, payload: string | UserCreds | null): Promis }); - if (!payload) { - socket.send('no-token'); - } else if (payload instanceof String || typeof payload === 'string') { - socket.send(payload); + if (token) { + socket.send(token); } else { - socket.send(JSON.stringify(payload)); + socket.send("no-token"); } }); }; -const onMessage = (messageStr: string): void => { - while (!auth) { - backgroundMitt.emit('auth-res', messageStr); - return; - } +//---Message Handlers----------------------------------------------- - // Decode the message. - const m = JSON.parse(messageStr) - let message: RenderMessage | Annotation; +const handleAuthMessage = (message: string) => { + backgroundMitt.emit('auth-res', message); +} - // Check to see if this is a Render Message. - if (m.content) { - message = renderMessage(m.content.text, m.content.audio, m.context, m.modifier); +const handleProfileMessage = (message: Profile) => { + backgroundMitt.emit('ipc-renderer', { + endpoint: 'update-profile', + message: message + }); +} - // Play audio if any. - if (message.content.audio) { - const audioBytes = Buffer.from(m.content.audio as string, 'hex'); - m.content.audio = true; - play(audioBytes); - } +const handleStandardMessage = (m: StandardMessage) => { - } + // Create a render message object. + const message = renderMessage( + m.content.text, m.content.audio, m.context, m.modifier); - else { - message = m // Annotation + // Play audio if any. + if (message.content.audio) { + const audioBytes = Buffer.from(m.content.audio as string, 'hex'); + m.content.audio = true; + play(audioBytes); } - // Send it to the frontend. backgroundMitt.emit('ipc-renderer', { endpoint: 'render-message', message: message }); +} + +const handleAnnotationMessage = (message: Annotation) => { + backgroundMitt.emit('ipc-renderer', { + endpoint: 'annotate-message', + message: message + }); +} + +//------------------------------------------------------------------ + +const onMessage = (messageStr: string): void => { + + // Auth messages are strings. + if (!auth) handleAuthMessage(messageStr) + + const message = JSON.parse(messageStr) + + if (message.content) { + handleStandardMessage(message) + } + + else if (message.first) { + handleProfileMessage(message) + } + + else { + handleAnnotationMessage(message) + } }; @@ -151,7 +186,6 @@ export function initSession(): void { socket.on("message", onMessage); } - export function sendAudio(audio: string, uid: string): void { const m = clientMessage("", audio, uid); socket.send(JSON.stringify(m)); diff --git a/src/components/inputItem/inputItem.vue b/src/components/inputItem/inputItem.vue index 05b859c..5050a78 100644 --- a/src/components/inputItem/inputItem.vue +++ b/src/components/inputItem/inputItem.vue @@ -5,7 +5,8 @@ :class="{ playing: recording }" :style="{ top: `${elementY}px`, left: `${elementX}px` }" > - AG +
{{ initials }}
+ @@ -22,14 +23,16 @@