71 lines
1.5 KiB
TypeScript
71 lines
1.5 KiB
TypeScript
|
|
// import useAudio from "@/audio";
|
|
import UIState from "@/uiState";
|
|
import { config } from "@/config";
|
|
import useWebsockets from "./composables/useWebsockets";
|
|
import { backgroundMitt, ipcEmit } from "@/composables/useEmitter";
|
|
|
|
// UI state
|
|
const uiState = new UIState()
|
|
|
|
// let audio = new Audio();
|
|
|
|
|
|
const onMessageCallback = (payload: string) => {
|
|
|
|
if (payload === "CLOSE_AUTH_FAIL") {
|
|
backgroundMitt.emit("close-auth-fail");
|
|
return
|
|
}
|
|
|
|
const message = JSON.parse(payload) as ClientProtocol;
|
|
|
|
if (message.header === "init") {
|
|
uiState.set(message.body as Init);
|
|
} else uiState.update(message.body as Update);
|
|
|
|
}
|
|
|
|
const connectionStatusCallback = (status: string) => {
|
|
ipcEmit('set-connection-status', status);
|
|
}
|
|
|
|
const statusOptions = {
|
|
openMessage: "Connected",
|
|
pongMessage: "Nominal",
|
|
closeMessage: "Reconnecting",
|
|
pingErrorMessage: "Connection Lost",
|
|
}
|
|
|
|
const { connect, send, close } = useWebsockets(
|
|
onMessageCallback,
|
|
connectionStatusCallback,
|
|
statusOptions
|
|
);
|
|
|
|
export const updateAppUI = (): void => {
|
|
uiState.emit();
|
|
}
|
|
|
|
export function sendMessage<Message>(message: Raw | Request): void {
|
|
send(message);
|
|
}
|
|
|
|
/* launch a new session (the main process for authenticated users) */
|
|
export function launchSession(platformKey: string) {
|
|
|
|
/* connect to the platform */
|
|
connect(config.PLATFORM_URL, platformKey);
|
|
|
|
// initialize the audio streams
|
|
// audio.record();
|
|
|
|
}
|
|
|
|
export function endSession() {
|
|
|
|
close(1000, 'session-logout');
|
|
|
|
uiState.reset();
|
|
|
|
}
|