25 lines
765 B
Python
25 lines
765 B
Python
|
|
"""
|
||
|
|
WebSocket connection manager — handles multiple simultaneous clients.
|
||
|
|
"""
|
||
|
|
from fastapi import WebSocket
|
||
|
|
from typing import List
|
||
|
|
|
||
|
|
class ConnectionManager:
|
||
|
|
def __init__(self):
|
||
|
|
self.active_connections: List[WebSocket] = []
|
||
|
|
|
||
|
|
async def connect(self, websocket: WebSocket):
|
||
|
|
await websocket.accept()
|
||
|
|
self.active_connections.append(websocket)
|
||
|
|
|
||
|
|
def disconnect(self, websocket: WebSocket):
|
||
|
|
self.active_connections.remove(websocket)
|
||
|
|
|
||
|
|
async def broadcast_json(self, data: dict):
|
||
|
|
for connection in self.active_connections:
|
||
|
|
await connection.send_json(data)
|
||
|
|
|
||
|
|
async def broadcast_bytes(self, data: bytes):
|
||
|
|
for connection in self.active_connections:
|
||
|
|
await connection.send_bytes(data)
|