-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebsocket_manager.py
More file actions
59 lines (46 loc) · 2.22 KB
/
websocket_manager.py
File metadata and controls
59 lines (46 loc) · 2.22 KB
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
from fastapi import WebSocket
from typing import Dict, List
import json
import logging
logger = logging.getLogger(__name__)
class ConnectionManager:
"""Manages active WebSocket connections for RFQ updates."""
def __init__(self):
self.active_connections: Dict[str, List[WebSocket]] = {}
async def connect(self, websocket: WebSocket, rfq_id: str):
"""Accepts a connection and tracks it by RFQ ID."""
await websocket.accept()
if rfq_id not in self.active_connections:
self.active_connections[rfq_id] = []
self.active_connections[rfq_id].append(websocket)
logger.info(f"[WS] Client connected to RFQ {rfq_id}. Total: {len(self.active_connections[rfq_id])}")
def disconnect(self, websocket: WebSocket, rfq_id: str):
"""Removes a connection from the tracking list."""
if rfq_id in self.active_connections:
if websocket in self.active_connections[rfq_id]:
self.active_connections[rfq_id].remove(websocket)
if not self.active_connections[rfq_id]:
del self.active_connections[rfq_id]
logger.info(f"[WS] Client disconnected from RFQ {rfq_id}.")
async def broadcast_to_rfq(self, rfq_id: str, message: dict):
"""Broadcasts a message to all connections for a specific RFQ."""
if rfq_id not in self.active_connections:
return
# Ensure the message is JSON-serializable
try:
safe_message = json.loads(json.dumps(message, default=str))
except Exception as e:
logger.error(f"[WS] Failed to serialize message for {rfq_id}: {e}")
return
dead_connections = []
for connection in self.active_connections[rfq_id]:
try:
await connection.send_json(safe_message)
except Exception as e:
logger.warning(f"[WS] Failed to send to client on {rfq_id}: {e}")
dead_connections.append(connection)
for dead in dead_connections:
self.disconnect(dead, rfq_id)
if self.active_connections.get(rfq_id):
logger.info(f"[WS] Broadcast to {len(self.active_connections[rfq_id])} client(s) on {rfq_id}")
manager = ConnectionManager()