From 0dec0015a59bd3d053988d8b7f2e1696ad0bff76 Mon Sep 17 00:00:00 2001 From: BennyKok Date: Mon, 18 Dec 2023 13:23:35 +0800 Subject: [PATCH] fix(plugin): a try catch for webscoket send --- custom_routes.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/custom_routes.py b/custom_routes.py index 5911cff..2bbffe1 100644 --- a/custom_routes.py +++ b/custom_routes.py @@ -132,13 +132,18 @@ async def websocket_handler(request): return ws async def send(event, data, sid=None): - if sid: - ws = sockets.get(sid) - if ws: - await ws.send_json({ 'event': event, 'data': data }) - else: - for ws in sockets.values(): - await ws.send_json({ 'event': event, 'data': data }) + try: + if sid: + ws = sockets.get(sid) + if ws and not ws.closed: # Check if the WebSocket connection is open and not closing + await ws.send_json({ 'event': event, 'data': data }) + else: + for ws in sockets.values(): + if not ws.closed: # Check if the WebSocket connection is open and not closing + await ws.send_json({ 'event': event, 'data': data }) + except Exception as e: + print(f"Exception: {e}") + traceback.print_exc() logging.basicConfig(level=logging.INFO)