Переглянути джерело

clear timer on disconnect

Falko Schindler 2 роки тому
батько
коміт
dbf1c04403
2 змінених файлів з 9 додано та 0 видалено
  1. 2 0
      nicegui/functions/timer.py
  2. 7 0
      nicegui/nicegui.py

+ 2 - 0
nicegui/functions/timer.py

@@ -43,6 +43,8 @@ class Timer:
 
     async def _run_in_loop(self) -> None:
         while True:
+            if self.client.id not in globals.clients:
+                return
             try:
                 start = time.time()
                 if self.active:

+ 7 - 0
nicegui/nicegui.py

@@ -63,6 +63,13 @@ async def handle_connect(sid: str, _) -> None:
     sio.enter_room(sid, str(client.id))
 
 
+@sio.on('disconnect')
+async def handle_disconnect(sid: str) -> None:
+    client = get_client(sid)
+    if client.id != 0:
+        del globals.clients[client.id]
+
+
 @sio.on('event')
 def handle_event(sid: str, msg: Dict) -> None:
     client = get_client(sid)