messenger v69
This commit is contained in:
parent
888651b4d7
commit
aeb212b5c9
10 changed files with 172 additions and 121 deletions
|
|
@ -12,7 +12,7 @@ let record = false;
|
|||
const audioContainer = {
|
||||
input: '',
|
||||
}
|
||||
const encoding = "base64";
|
||||
const encoding = "hex";
|
||||
const audioOptions = {
|
||||
channelCount: 1,
|
||||
sampleFormat: 16,
|
||||
|
|
@ -22,10 +22,13 @@ const audioOptions = {
|
|||
}
|
||||
|
||||
// callback run on space bar key up and down.
|
||||
const updateRecorder = (_event, payload: boolean): void => {
|
||||
record = payload;
|
||||
const updateRecorder = (_event, payload: {
|
||||
isRecording: boolean;
|
||||
uid: string | null;
|
||||
}): void => {
|
||||
record = payload.isRecording;
|
||||
if (!record) {
|
||||
sendAudio(Buffer.from(audioContainer.input, 'base64'));
|
||||
if (payload.uid) sendAudio(audioContainer.input, payload.uid);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -4,26 +4,9 @@ import WebSocket from 'ws';
|
|||
import { backgroundMitt } from './emitter';
|
||||
import { ipcMain } from "electron";
|
||||
import { play } from './audio';
|
||||
|
||||
interface RenderMessage {
|
||||
content: {
|
||||
text: string;
|
||||
audio: string;
|
||||
};
|
||||
context: string;
|
||||
modifier: string;
|
||||
id: string;
|
||||
}
|
||||
|
||||
interface UserCreds {
|
||||
email: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
interface TextMessage {
|
||||
audio: 0;
|
||||
content: string;
|
||||
}
|
||||
import { UserCreds, ClientMessage } from "@/types/message/index";
|
||||
import { clientMessage } from '@/modules/message';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
|
||||
const ip = 'ws://127.0.0.1';
|
||||
const port = 8760;
|
||||
|
|
@ -32,8 +15,7 @@ let socket: WebSocket;
|
|||
let success = false;
|
||||
let auth = false;
|
||||
|
||||
|
||||
const authSession = async (_event, payload: string | UserCreds | null): Promise<string> => {
|
||||
const onAuthSession = async (_event, payload: string | UserCreds | null): Promise<string> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
console.log('authenticating...');
|
||||
|
|
@ -61,18 +43,7 @@ const authSession = async (_event, payload: string | UserCreds | null): Promise<
|
|||
});
|
||||
};
|
||||
|
||||
// function parseHexString(str: string) {
|
||||
// var result = [];
|
||||
// while (str.length >= 8) {
|
||||
// result.push(parseInt(str.substring(0, 8), 16));
|
||||
|
||||
// str = str.substring(8, str.length);
|
||||
// }
|
||||
|
||||
// return result;
|
||||
// }
|
||||
|
||||
const receiveMessage = (message: string): void => {
|
||||
const onMessage = (message: string): void => {
|
||||
|
||||
while (!auth) {
|
||||
backgroundMitt.emit('auth-res', message);
|
||||
|
|
@ -80,34 +51,34 @@ const receiveMessage = (message: string): void => {
|
|||
}
|
||||
|
||||
// Parse the message.
|
||||
const parsed: RenderMessage = JSON.parse(message);
|
||||
console.log('message received', parsed.content.text);
|
||||
const m = JSON.parse(message);
|
||||
|
||||
// If there is audio, play it.
|
||||
if (parsed.content.audio) {
|
||||
const audioBytes = Buffer.from(parsed.content.audio, 'hex');
|
||||
play(audioBytes);
|
||||
// check to see if this is a Render Message.
|
||||
if (m.content) {
|
||||
// If there is audio, play it.
|
||||
if (m.content.audio) {
|
||||
const audioBytes = Buffer.from(m.content.audio as string, 'hex');
|
||||
play(audioBytes);
|
||||
}
|
||||
|
||||
// if there is no unique id, add it.
|
||||
if(!m.uid) m.uid = uuidv4();
|
||||
}
|
||||
|
||||
backgroundMitt.emit('ipc-renderer', {
|
||||
endpoint: 'render-message',
|
||||
message: parsed
|
||||
message: m
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
const sendMessage = (_event, payload: TextMessage): void => {
|
||||
console.log('sending message')
|
||||
const onClientMessage = (_event, payload: ClientMessage): void => {
|
||||
console.log('sending message');
|
||||
socket.send(JSON.stringify(payload));
|
||||
}
|
||||
|
||||
export const sendAudio = (content: Buffer) => {
|
||||
console.log('sending audio')
|
||||
socket.send(content);
|
||||
}
|
||||
|
||||
// Run every time we want to connect to backend.
|
||||
export const initSession = () => {
|
||||
export function initSession(): void {
|
||||
|
||||
// Close existing sockets.
|
||||
if (socket) {
|
||||
|
|
@ -123,11 +94,11 @@ export const initSession = () => {
|
|||
|
||||
// handle renderer auth-token event
|
||||
ipcMain.removeHandler('auth-session'); // avoid setting duplicate handlers
|
||||
ipcMain.handle('auth-session', authSession);
|
||||
ipcMain.handle('auth-session', onAuthSession);
|
||||
|
||||
// handle user message event
|
||||
ipcMain.removeAllListeners('message');
|
||||
ipcMain.on('message', sendMessage);
|
||||
ipcMain.removeAllListeners('client-message');
|
||||
ipcMain.on('client-message', onClientMessage);
|
||||
|
||||
// Connect to the backend.
|
||||
socket.on('open', () => {
|
||||
|
|
@ -162,6 +133,10 @@ export const initSession = () => {
|
|||
initSession();
|
||||
});
|
||||
|
||||
socket.on("message", receiveMessage);
|
||||
};
|
||||
socket.on("message", onMessage);
|
||||
}
|
||||
|
||||
export function sendAudio(content: string, uid: string): void {
|
||||
const m = clientMessage("", content, uid);
|
||||
socket.send(JSON.stringify(m));
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue