Spaces:
Running
Running
File size: 2,466 Bytes
779968d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | import type { Settings } from "./types.ts";
type WebSocketMessage =
| { type: "init"; settings: Settings }
| { type: "reset" }
| { type: "next-step" }
| { type: "prev-step" }
| { type: "play" }
| { type: "pause" };
const RECONNECT_INTERVAL = 3000; // 3 seconds
export default class WebSocketApi {
private socket: WebSocket | null = null;
private onMessageHandler: ((data: unknown) => void) | null = null;
private onReconnectHandler: (() => void) | null = null;
private pendingMessages: WebSocketMessage[] = [];
connect(url: string) {
this.socket = new WebSocket(url);
this.socket.onopen = () => {
console.log("WebSocket connection established.");
if (this.onReconnectHandler) {
this.onReconnectHandler();
}
for (const message of this.pendingMessages) {
this.sendMessage(message);
}
this.pendingMessages = [];
}
this.socket.onclose = () => {
console.log("WebSocket connection closed - reconnecting...");
setTimeout(() => {
this.connect(url);
}, RECONNECT_INTERVAL);
};
this.socket.onerror = (error) => {
console.error("WebSocket error:", error);
};
this.socket.onmessage = (event) => {
if (!this.onMessageHandler) return;
try {
const data = JSON.parse(event.data);
this.onMessageHandler(data);
} catch (parseError) {
console.error("WebSocket message parse error:", parseError);
}
};
}
disconnect() {
if (this.socket) {
this.socket.close();
this.socket = null;
}
}
sendInit(settings: Settings) {
this.sendMessage({ type: "init", settings });
}
sendReset() {
this.sendMessage({ type: "reset" });
}
sendNextStep() {
this.sendMessage({ type: "next-step" });
}
sendPrevStep() {
this.sendMessage({ type: "prev-step" });
}
sendPlay() {
this.sendMessage({ type: "play" });
}
sendPause() {
this.sendMessage({ type: "pause" });
}
onMessage(handler: (data: unknown) => void) {
this.onMessageHandler = handler;
}
onReconnect(handler: () => void) {
this.onReconnectHandler = handler;
}
private sendMessage(message: WebSocketMessage) {
if (this.socket?.readyState === WebSocket.OPEN) {
this.socket.send(JSON.stringify(message));
} else {
console.log("WebSocket not connected, queuing message:", message);
this.pendingMessages.push(message);
}
}
}
|