-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathws_manager.py
More file actions
34 lines (27 loc) · 979 Bytes
/
ws_manager.py
File metadata and controls
34 lines (27 loc) · 979 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import json
from fastapi import WebSocket
class ConnectionManager:
def __init__(self):
self.active: dict[str, list[WebSocket]] = {}
async def connect(self, meeting_id: str, ws: WebSocket):
await ws.accept()
if meeting_id not in self.active:
self.active[meeting_id] = []
self.active[meeting_id].append(ws)
def disconnect(self, meeting_id: str, ws: WebSocket):
if meeting_id in self.active:
self.active[meeting_id] = [
w for w in self.active[meeting_id] if w != ws
]
async def broadcast(self, meeting_id: str, data: dict):
if meeting_id not in self.active:
return
dead = []
for ws in self.active[meeting_id]:
try:
await ws.send_json(data)
except Exception:
dead.append(ws)
for ws in dead:
self.active[meeting_id].remove(ws)
manager = ConnectionManager()