From 22fa56989893bd14725bcd66a5db072a040a7d4b Mon Sep 17 00:00:00 2001 From: Michael Maltese Date: Mon, 25 Jan 2021 10:23:28 -0800 Subject: [PATCH] use native websocket ping frames rather than JSON ping MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Code was sending a JSON message {"ping":"0123456789"} to test if the connection was still open — WebSocket has native support for this. This is easier for clients as the native ping frames are usually handled automatically by the WebSocket library. Note that gevent-websocket doesn't automatically send out ping frames to keep the connection alive, which servers are supposed to do. It might be worth switching to another WebSocket library in the future that is more compliant. --- warpserver/sockets/__init__.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/warpserver/sockets/__init__.py b/warpserver/sockets/__init__.py index ee12ab0..b23f2cd 100644 --- a/warpserver/sockets/__init__.py +++ b/warpserver/sockets/__init__.py @@ -8,14 +8,17 @@ def refresh_ws_list(): - dead_sockets = list() - for user in ws_list: + dead_users = list() + for user, socket in ws_list.items(): + if socket.closed: + dead_users.append(user) + continue try: - ws_list[user].send('{"ping":"0123456789"}') - except WebSocketError: - dead_sockets.append(user) - for socket in dead_sockets: - del ws_list[socket] + socket.send_frame("0123456789", socket.OPCODE_PING) + except WebSocketError as e: + dead_users.append(user) + for user in dead_users: + del ws_list[user] def check_auth(ws):