Spaces:
Sleeping
Sleeping
Download app/websockets/manager.py from Sherry245/rideshare-api: direct link, hf CLI and curl.
- Browser
- Download file 535 Bytes
-
https://huggingface.co/spaces/Sherry245/rideshare-api/resolve/main/app/websockets/manager.py
- Command line
-
hf download hf://spaces/Sherry245/rideshare-api/app/websockets/manager.py
-
curl -L -o manager.py https://huggingface.co/spaces/Sherry245/rideshare-api/resolve/main/app/websockets/manager.py
535 Bytes
| from fastapi import WebSocket | |
| from typing import Dict | |
| class ConnectionManager: | |
| def __init__(self): | |
| self.active: Dict[str, WebSocket] = {} | |
| async def connect(self, ws: WebSocket, user_id: str): | |
| await ws.accept() | |
| self.active[user_id] = ws | |
| def disconnect(self, user_id: str): | |
| self.active.pop(user_id, None) | |
| async def send(self, user_id: str, message: dict): | |
| ws = self.active.get(user_id) | |
| if ws: | |
| await ws.send_json(message) | |
| manager = ConnectionManager() | |