Spaces:
Paused
Paused
Download backend/api/websocket.py from Kyoiske/GOAT-Evolution: direct link, hf CLI and curl.
- Browser
- Download file 762 Bytes
-
https://huggingface.co/spaces/Kyoiske/GOAT-Evolution/resolve/main/backend/api/websocket.py
- Command line
-
hf download hf://spaces/Kyoiske/GOAT-Evolution/backend/api/websocket.py
-
curl -L -o websocket.py https://huggingface.co/spaces/Kyoiske/GOAT-Evolution/resolve/main/backend/api/websocket.py
762 Bytes
| from __future__ import annotations | |
| import asyncio | |
| from fastapi import APIRouter, WebSocket, WebSocketDisconnect | |
| from backend.core.event_bus import event_bus | |
| router = APIRouter() | |
| async def battle_ws(websocket: WebSocket, session_id: str): | |
| await websocket.accept() | |
| queue = await event_bus.subscribe(session_id) | |
| try: | |
| while True: | |
| try: | |
| message = await asyncio.wait_for(queue.get(), timeout=30) | |
| await websocket.send_json(message) | |
| except asyncio.TimeoutError: | |
| await websocket.send_json({"event": "ping", "session_id": session_id, "payload": {}}) | |
| except WebSocketDisconnect: | |
| event_bus.unsubscribe(queue, session_id) | |