24 lines
796 B
Python
24 lines
796 B
Python
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
|
|
from typing import List
|
|
|
|
class ConnectionManager:
|
|
def __init__(self):
|
|
self.active_connections: List[WebSocket] = []
|
|
|
|
async def connect(self, websocket: WebSocket):
|
|
self.active_connections.append(websocket)
|
|
|
|
async def disconnect(self, websocket: WebSocket):
|
|
if websocket in self.active_connections:
|
|
self.active_connections.remove(websocket)
|
|
|
|
async def broadcast_raw(self, data: bytes):
|
|
for connection in self.active_connections:
|
|
try:
|
|
await connection.send_bytes(data)
|
|
except WebSocketDisconnect:
|
|
await self.disconnect(connection)
|
|
except Exception as e:
|
|
print(f"Error sending to client: {e}")
|
|
|