File size: 1,955 Bytes
d8bad25 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | """
WebSocket connection manager for broadcasting real-time data
to all connected frontend clients.
"""
import json
import asyncio
from fastapi import WebSocket
from datetime import datetime
class ConnectionManager:
"""Manages WebSocket connections and broadcasts messages."""
def __init__(self):
self.active_connections: list[WebSocket] = []
async def connect(self, websocket: WebSocket):
await websocket.accept()
self.active_connections.append(websocket)
print(f"[WS] Client connected. Total: {len(self.active_connections)}")
def disconnect(self, websocket: WebSocket):
if websocket in self.active_connections:
self.active_connections.remove(websocket)
print(f"[WS] Client disconnected. Total: {len(self.active_connections)}")
async def broadcast(self, message_type: str, data: dict):
"""Broadcast a message to all connected clients."""
message = json.dumps({
"type": message_type,
"data": data,
"timestamp": datetime.now().isoformat()
})
disconnected = []
for connection in self.active_connections:
try:
await connection.send_text(message)
except Exception:
disconnected.append(connection)
for conn in disconnected:
self.disconnect(conn)
async def send_personal(self, websocket: WebSocket, message_type: str, data: dict):
"""Send a message to a specific client."""
message = json.dumps({
"type": message_type,
"data": data,
"timestamp": datetime.now().isoformat()
})
try:
await websocket.send_text(message)
except Exception:
self.disconnect(websocket)
@property
def client_count(self) -> int:
return len(self.active_connections)
|