牧安云哨-后端

This commit is contained in:
BBIT-Kai
2025-12-23 09:55:06 +08:00
parent f018d96e60
commit b5a9714025
16 changed files with 1219 additions and 75 deletions
+25
View File
@@ -0,0 +1,25 @@
import asyncio
from typing import List
from fastapi import WebSocket
class ConnectionManager:
def __init__(self):
self.active_connections: List[WebSocket] = []
self.lock = asyncio.Lock()
async def connect(self, websocket: WebSocket):
await websocket.accept()
async with self.lock:
self.active_connections.append(websocket)
async def disconnect(self, websocket: WebSocket):
async with self.lock:
if websocket in self.active_connections:
self.active_connections.remove(websocket)
async def broadcast(self, message: dict):
async with self.lock:
for ws in self.active_connections:
await ws.send_json(message)