Catch websocket disconnect (#184)
This commit is contained in:
parent
af9733996a
commit
a93b1425c7
@ -4,6 +4,7 @@ from typing import Any, Callable, Coroutine, Dict, List, Optional, Tuple, Type,
|
|||||||
|
|
||||||
from fastapi import FastAPI, WebSocket
|
from fastapi import FastAPI, WebSocket
|
||||||
from fastapi.middleware import cors
|
from fastapi.middleware import cors
|
||||||
|
from starlette.websockets import WebSocketDisconnect
|
||||||
|
|
||||||
from pynecone import constants, utils
|
from pynecone import constants, utils
|
||||||
from pynecone.base import Base
|
from pynecone.base import Base
|
||||||
@ -320,7 +321,11 @@ def _event(app: App):
|
|||||||
# Process events until the connection is closed.
|
# Process events until the connection is closed.
|
||||||
while True:
|
while True:
|
||||||
# Get the event.
|
# 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.
|
# Process the event.
|
||||||
update = await process(app, event)
|
update = await process(app, event)
|
||||||
|
Loading…
Reference in New Issue
Block a user