From 4eaf9b56f721e15eaf446f9f48ab68e0a79583a1 Mon Sep 17 00:00:00 2001 From: n9k Date: Wed, 9 Mar 2022 05:15:21 +0000 Subject: [PATCH] Try to ensure websocket is closed when forgetting about it Might not be necessary, but if it is then it prevents a sitation where a websocket is still open but we've forgotten about it, so we will never broadcast any new messages to it and the client will be practically frozen in time until they disconnect and open a new websocket. Also update the user's last_seen when the websocket is closed. This prevents a user with js enabled who's actually idle being considered absent and being rotated when their websocket accidentally closes for a few seconds. --- anonstream/routes/websocket.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/anonstream/routes/websocket.py b/anonstream/routes/websocket.py index 1734f21..158561a 100644 --- a/anonstream/routes/websocket.py +++ b/anonstream/routes/websocket.py @@ -5,6 +5,7 @@ import asyncio from quart import current_app, websocket +from anonstream.user import see from anonstream.websocket import websocket_outbound, websocket_inbound from anonstream.routes.wrappers import with_user_from @@ -19,4 +20,9 @@ async def live(user): try: await asyncio.gather(producer, consumer) finally: + see(user) user['websockets'].remove(queue) + try: + await websocket.close(1000) + except RuntimeError: + pass