Implement viewer_{dis,}connect events

This commit is contained in:
2022-09-23 00:31:18 +02:00
parent fb22cc7528
commit d1df4ff6a2
3 changed files with 60 additions and 19 deletions

View File

@@ -31,6 +31,18 @@ class WSManager:
self.viewers: dict[UUID, set[WebSocket]] = dict()
self.queue: asyncio.Queue[tuple[UUID, any]] = asyncio.Queue()
async def send_connect(self, uuid: UUID):
if uuid not in self.computers:
return
await self.computers[uuid].send_json({"type": "viewer_connect"})
async def send_disconnect(self, uuid: UUID):
if uuid not in self.computers:
return
await self.computers[uuid].send_json({"type": "viewer_disconnect"})
async def queue_task(self):
print("[WS] queue task started")
while True:
@@ -53,6 +65,10 @@ class WSManager:
print(f"[WS] Computer {uuid} connected")
self.computers[uuid] = socket
if len(self.viewers.get(uuid, [])) > 0:
await self.send_connect(uuid)
while True:
try:
data = await socket.receive_json()
@@ -76,6 +92,9 @@ class WSManager:
if uuid not in self.viewers:
self.viewers[uuid] = set()
if len(self.viewers[uuid]) == 0:
await self.send_connect(uuid)
self.viewers[uuid].add(socket)
while True:
@@ -83,8 +102,11 @@ class WSManager:
data = await socket.receive_json()
except WebSocketDisconnect:
break
self.viewers[uuid].remove(socket)
if len(self.viewers[uuid]) == 0:
await self.send_disconnect(uuid)
print(f"[WS] Browser disconnected for {uuid}")