Bläddra i källkod

Catch websocket disconnect (#184)

Nikhil Rao 2 år sedan
förälder
incheckning
a93b1425c7
1 ändrade filer med 6 tillägg och 1 borttagningar
  1. 6 1
      pynecone/app.py

+ 6 - 1
pynecone/app.py

@@ -4,6 +4,7 @@ from typing import Any, Callable, Coroutine, Dict, List, Optional, Tuple, Type,
 
 from fastapi import FastAPI, WebSocket
 from fastapi.middleware import cors
+from starlette.websockets import WebSocketDisconnect
 
 from pynecone import constants, utils
 from pynecone.base import Base
@@ -320,7 +321,11 @@ def _event(app: App):
         # Process events until the connection is closed.
         while True:
             # Get the event.
-            event = Event.parse_raw(await websocket.receive_text())
+            try:
+                event = Event.parse_raw(await websocket.receive_text())
+            except WebSocketDisconnect:
+                # Close the connection.
+                return
 
             # Process the event.
             update = await process(app, event)