file_table_reader/api/routers/ws/manager.py
2026-02-24 08:47:34 +07:00

24 lines
706 B
Python
Executable File

from typing import Dict, List
from fastapi import WebSocket
class JobWSManager:
def __init__(self):
self.connections: Dict[str, List[WebSocket]] = {}
async def connect(self, job_id: str, ws: WebSocket):
await ws.accept()
self.connections.setdefault(job_id, []).append(ws)
def disconnect(self, job_id: str, ws: WebSocket):
if job_id in self.connections:
self.connections[job_id].remove(ws)
if not self.connections[job_id]:
del self.connections[job_id]
async def send(self, job_id: str, data: dict):
for ws in self.connections.get(job_id, []):
await ws.send_json(data)
manager = JobWSManager()