mirror of
https://github.com/jihe520/MathModelAgent.git
synced 2026-10-02 02:44:56 +08:00
28 lines
858 B
Python
28 lines
858 B
Python
from fastapi import WebSocket
|
|
|
|
|
|
class WebSocketManager:
|
|
def __init__(self):
|
|
self.active_connections: list[WebSocket] = []
|
|
|
|
async def connect(self, websocket: WebSocket):
|
|
await websocket.accept()
|
|
self.active_connections.append(websocket)
|
|
|
|
def disconnect(self, websocket: WebSocket):
|
|
if websocket in self.active_connections:
|
|
self.active_connections.remove(websocket)
|
|
|
|
async def send_personal_message(self, message: str, websocket: WebSocket):
|
|
await websocket.send_text(message)
|
|
|
|
async def send_personal_message_json(self, message: dict, websocket: WebSocket):
|
|
await websocket.send_json(message)
|
|
|
|
async def broadcast(self, message: str):
|
|
for connection in list(self.active_connections):
|
|
await connection.send_text(message)
|
|
|
|
|
|
ws_manager = WebSocketManager()
|