Spaces:
Paused
Paused
Download frontend/src/hooks/useBattleWebSocket.ts from Kyoiske/GOAT-Evolution: direct link, hf CLI and curl.
- Browser
- Download file 975 Bytes
-
https://huggingface.co/spaces/Kyoiske/GOAT-Evolution/resolve/main/frontend/src/hooks/useBattleWebSocket.ts
- Command line
-
hf download hf://spaces/Kyoiske/GOAT-Evolution/frontend/src/hooks/useBattleWebSocket.ts
-
curl -L -o useBattleWebSocket.ts https://huggingface.co/spaces/Kyoiske/GOAT-Evolution/resolve/main/frontend/src/hooks/useBattleWebSocket.ts
975 Bytes
| import { useEffect } from "react"; | |
| import { useBattleStore } from "../stores/battleStore"; | |
| export function useBattleWebSocket(sessionId?: string) { | |
| const pushEvent = useBattleStore((s) => s.pushEvent); | |
| const setStatus = useBattleStore((s) => s.setStatus); | |
| const setError = useBattleStore((s) => s.setError); | |
| useEffect(() => { | |
| if (!sessionId) return; | |
| const proto = window.location.protocol === "https:" ? "wss" : "ws"; | |
| const ws = new WebSocket(`${proto}://${window.location.host}/ws/battle/${sessionId}`); | |
| setStatus("running"); | |
| ws.onmessage = (evt) => { | |
| try { | |
| const payload = JSON.parse(evt.data); | |
| if (payload?.event === "ping") return; | |
| pushEvent(payload); | |
| } catch { | |
| // Ignore malformed packets. | |
| } | |
| }; | |
| ws.onerror = () => setError("WebSocket disconnected."); | |
| ws.onclose = () => setStatus("stopped"); | |
| return () => ws.close(); | |
| }, [sessionId, pushEvent, setError, setStatus]); | |
| } | |