142 lines
3.2 KiB
TypeScript
142 lines
3.2 KiB
TypeScript
"use strict";
|
|
|
|
import WebSocket from 'ws';
|
|
import { backgroundMitt } from './emitter';
|
|
import { ipcMain } from "electron";
|
|
|
|
interface RenderMessage {
|
|
content: string;
|
|
context: string;
|
|
subContext: string;
|
|
modifier: string;
|
|
time: string;
|
|
id: string;
|
|
}
|
|
|
|
interface UserCreds {
|
|
email: string;
|
|
password: string;
|
|
}
|
|
|
|
interface TextMessage {
|
|
audio: 0;
|
|
content: string;
|
|
}
|
|
|
|
const ip = 'ws://127.0.0.1';
|
|
const port = 8760;
|
|
const reconnectTimeout = 3000; //ms
|
|
let socket: WebSocket;
|
|
let success = false;
|
|
let auth = false;
|
|
|
|
const authSession = async (_event, payload: string | UserCreds | null): Promise<string> => {
|
|
return new Promise((resolve, reject) => {
|
|
|
|
console.log('authenticating...');
|
|
|
|
backgroundMitt.once('auth-res', (res: string) => {
|
|
|
|
if (res === 'locked') {
|
|
reject(res);
|
|
} else {
|
|
console.log('Success!');
|
|
auth = true;
|
|
resolve(res);
|
|
}
|
|
|
|
});
|
|
|
|
if (!payload) {
|
|
socket.send('no-token');
|
|
} else if (payload instanceof String || typeof payload === 'string') {
|
|
socket.send(payload);
|
|
} else {
|
|
socket.send(JSON.stringify(payload));
|
|
}
|
|
|
|
});
|
|
};
|
|
|
|
const receiveMessage = (message: string): void => {
|
|
|
|
while (!auth) {
|
|
backgroundMitt.emit('auth-res', message);
|
|
return;
|
|
}
|
|
|
|
const parsed: RenderMessage = JSON.parse(message);
|
|
backgroundMitt.emit('ipc-renderer', {
|
|
endpoint: 'render-message',
|
|
message: parsed
|
|
});
|
|
|
|
};
|
|
|
|
const sendMessage = (_event, payload: TextMessage): void => {
|
|
socket.send(JSON.stringify(payload));
|
|
}
|
|
|
|
export const sendAudio = (content: Buffer) => {
|
|
socket.send(content);
|
|
}
|
|
|
|
// Run every time we want to connect to backend.
|
|
export const initSession = () => {
|
|
|
|
// Close existing sockets.
|
|
if (socket) {
|
|
socket.removeAllListeners();
|
|
socket.terminate();
|
|
socket.close();
|
|
success = false;
|
|
}
|
|
|
|
// Init new socket.
|
|
socket = new WebSocket(`${ip}:${port}`);
|
|
socket.binaryType = 'arraybuffer';
|
|
|
|
// handle renderer auth-token event
|
|
ipcMain.removeHandler('auth-session'); // avoid setting duplicate handlers
|
|
ipcMain.handle('auth-session', authSession);
|
|
|
|
// handle user message event
|
|
ipcMain.removeAllListeners('message');
|
|
ipcMain.on('message', sendMessage);
|
|
|
|
// Connect to the backend.
|
|
socket.on('open', () => {
|
|
console.log('Connected to Crimata Servers.');
|
|
|
|
// call auth renderer event on connection
|
|
backgroundMitt.emit('ipc-renderer', {
|
|
endpoint: 'auth',
|
|
message: null
|
|
});
|
|
|
|
success = true;
|
|
|
|
});
|
|
|
|
// Handle for failed connect, try again.
|
|
socket.on('error', (_e) => {
|
|
console.log('ERROR: Failed to connect.');
|
|
socket.removeAllListeners();
|
|
socket.close();
|
|
setTimeout(() => {
|
|
if (!success) {
|
|
console.log('Reconnecting...');
|
|
initSession();
|
|
}
|
|
}, reconnectTimeout);
|
|
|
|
});
|
|
|
|
socket.on('close', () => {
|
|
console.log('Connection droped! Restarting...')
|
|
initSession();
|
|
});
|
|
|
|
socket.on("message", receiveMessage);
|
|
};
|
|
|