Skip to content

Commit d529eb7

Browse files
author
Alan Christie
committed
feat: Fix disconnect behaviour
1 parent 94f7a4b commit d529eb7

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

app/app.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ async def event_stream(websocket: WebSocket, uuid: str):
182182
"Reading messages for %s (message_reader=%s)...", es_id, message_reader
183183
)
184184
_running: bool = True
185-
while _running:
185+
_connected: bool = True
186+
while _running and _connected:
186187
_LOGGER.debug("Calling anext() for %s...", es_id)
187188
reader = anext(message_reader)
188189
message_body = await reader
@@ -194,14 +195,19 @@ async def event_stream(websocket: WebSocket, uuid: str):
194195
try:
195196
await websocket.send_text(str(message_body))
196197
except WebSocketDisconnect:
197-
_LOGGER.warning("Got WebSocketDisconnect for %s (%s) (closing)...")
198-
_running = False
198+
_LOGGER.warning(
199+
"Got WebSocketDisconnect for %s (%s) (leaving)...", es_id, uuid
200+
)
201+
_connected = False
202+
203+
if _connected:
204+
_LOGGER.info("Closing %s (uuid=%s)...", es_id, uuid)
205+
await websocket.close(
206+
code=status.WS_1000_NORMAL_CLOSURE, reason="The stream has been deleted"
207+
)
208+
_LOGGER.info("Closed %s", es_id)
199209

200-
_LOGGER.info("Closing %s (uuid=%s)...", es_id, uuid)
201-
await websocket.close(
202-
code=status.WS_1000_NORMAL_CLOSURE, reason="The stream has been deleted"
203-
)
204-
_LOGGER.info("Closed %s", es_id)
210+
_LOGGER.info("Disconnected %s (uuid=%s)...", es_id, uuid)
205211

206212

207213
async def _get_from_queue(routing_key: str):

0 commit comments

Comments
 (0)