Spaces:
Paused
Paused
| from fastapi import FastAPI, WebSocket | |
| from fastapi.middleware.cors import CORSMiddleware | |
| app = FastAPI() | |
| # Allow CORS for local development | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| connected_clients = [] | |
| async def websocket_endpoint(websocket: WebSocket): | |
| await websocket.accept() | |
| connected_clients.append(websocket) | |
| try: | |
| while True: | |
| data = await websocket.receive_text() | |
| for client in connected_clients: | |
| if client != websocket: | |
| await client.send_text(data) | |
| except Exception as e: | |
| print(f"Client disconnected: {e}") | |
| finally: | |
| connected_clients.remove(websocket) | |