import { SimliClientEvents } from "../Events"; import { Logger } from "../Logger"; import { BaseSignaling } from "../Signaling/BaseSignaling"; type EventCallback = (...args: any[]) => void; type EventMap = Map>; interface BaseTransport { videoElementAnchor: HTMLVideoElement audioElementAnchor: HTMLAudioElement signalingConnection: BaseSignaling session_token: string events: EventMap; logger: Logger; connect(): Promise; disconnect(): void; on( event: K, callback: SimliClientEvents[K] ): void; off( event: K, callback: SimliClientEvents[K], ): void; emit( event: K, ...args: Parameters ): void; } function register_destination(logger: Logger, serialized_info: string) { const parsed = JSON.parse(serialized_info) logger.destination = parsed.destination logger.session_id = parsed.session_id } async function handleMessage(transport: BaseTransport, message: MessageEvent): Promise { const firstToken = (message.data as string).toUpperCase().split(" ")[0] switch (firstToken) { case "START": { // SOFT IGNORE break } case "ACK": { transport.emit("ack") break; } case "STOP": { transport.disconnect(); transport.emit("stop") break; } case "CLOSING": case "RATE": case "ERROR": case "ERROR:": { transport.disconnect() transport.emit("error", message.data as string) } case "SPEAK": { transport.emit("speaking"); break } case "SILENT": { transport.emit("silent"); break } default: { if (firstToken.includes("SDP") || firstToken.includes("LIVEKIT")) { transport.emit("connection_info", message.data) } else if (firstToken.includes("VIDEO_METADATA")) { transport.emit("video_info", message.data) } else if (firstToken.includes("ENDFRAME")) { transport.emit("stop") transport.disconnect() } else if (firstToken.includes("DESTINATION")) { transport.emit("destination", message.data) } else { transport.emit("unknown", message.data) } } } } export { handleMessage, register_destination }; export type { BaseTransport, EventCallback, EventMap };