You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

49 lines
1.8 KiB

from fastapi import WebSocket, WebSocketDisconnect
from typing import Dict, Set
import json
import logging
logger = logging.getLogger(__name__)
class WebSocketManager:
def __init__(self):
self.active_connections: Dict[str, Set[WebSocket]] = {}
async def connect(self, websocket: WebSocket, user_id: str):
await websocket.accept()
if user_id not in self.active_connections:
self.active_connections[user_id] = set()
self.active_connections[user_id].add(websocket)
logger.info(f"WebSocket 用户 {user_id} 已连接")
def disconnect(self, websocket: WebSocket, user_id: str):
if user_id in self.active_connections:
self.active_connections[user_id].discard(websocket)
if not self.active_connections[user_id]:
del self.active_connections[user_id]
logger.info(f"WebSocket 用户 {user_id} 已断开")
async def send_to_user(self, user_id: str, message: dict):
if user_id not in self.active_connections:
return False
dead_connections = set()
sent_count = 0
for connection in list(self.active_connections.get(user_id, set())):
try:
await connection.send_text(json.dumps(message, ensure_ascii=False))
sent_count += 1
except Exception:
dead_connections.add(connection)
for conn in dead_connections:
self.active_connections[user_id].discard(conn)
if not self.active_connections.get(user_id):
self.active_connections.pop(user_id, None)
return sent_count > 0
async def broadcast(self, message: dict):
for user_id in list(self.active_connections.keys()):
await self.send_to_user(user_id, message)
ws_manager = WebSocketManager()