Implement viewer_{dis,}connect events
This commit is contained in:
@@ -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}")
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user