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.
 
 
 

60 lines
2.5 KiB

from fastapi import WebSocket, WebSocketDisconnect
from typing import Dict, Set
import json
import logging
logger = logging.getLogger(__name__) # 当前模块的日志记录器
class WebSocketManager:
"""WebSocket 连接管理器。
管理用户与多个 WebSocket 连接的生命周期,
支持单用户多连接、定向推送和全局广播功能。
"""
def __init__(self):
"""初始化 WebSocket 连接池。"""
self.active_connections: Dict[str, Set[WebSocket]] = {} # 活跃连接池:{user_id: {WebSocket, ...}}
async def connect(self, websocket: WebSocket, user_id: str):
"""接受 WebSocket 连接并注册到指定用户的连接池中。"""
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):
"""断开 WebSocket 连接并从用户连接池中移除。"""
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):
"""向当前所有活跃用户广播消息,遍历所有用户并调用 send_to_user。"""
for user_id in list(self.active_connections.keys()):
await self.send_to_user(user_id, message)
ws_manager = WebSocketManager() # 全局 WebSocket 管理器单例