From d4e2b7552056ca61fad0569d3387116b9f8584a9 Mon Sep 17 00:00:00 2001 From: Fabian Dill Date: Mon, 2 Jan 2023 20:24:54 +0100 Subject: [PATCH] Clients: retry connection with ssl (#1341) --- CommonClient.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/CommonClient.py b/CommonClient.py index aa11661d..e3c325d2 100644 --- a/CommonClient.py +++ b/CommonClient.py @@ -494,7 +494,7 @@ class CommonContext: self._messagebox.open() return self._messagebox - def _handle_connection_loss(self, msg: str) -> None: + def handle_connection_loss(self, msg: str) -> None: """Helper for logging and displaying a loss of connection. Must be called from an except block.""" exc_info = sys.exc_info() logger.exception(msg, exc_info=exc_info, extra={'compact_gui': True}) @@ -580,14 +580,22 @@ async def server_loop(ctx: CommonContext, address: typing.Optional[str] = None) for msg in decode(data): await process_server_cmd(ctx, msg) logger.warning(f"Disconnected from multiworld server{reconnect_hint()}") + except websockets.InvalidMessage: + # probably encrypted + if address.startswith("ws://"): + await server_loop(ctx, "ws" + address[1:]) + else: + ctx.handle_connection_loss(f"Lost connection to the multiworld server due to InvalidMessage" + f"{reconnect_hint()}") except ConnectionRefusedError: - ctx._handle_connection_loss("Connection refused by the server. May not be running Archipelago on that address or port.") + ctx.handle_connection_loss("Connection refused by the server. " + "May not be running Archipelago on that address or port.") except websockets.InvalidURI: - ctx._handle_connection_loss("Failed to connect to the multiworld server (invalid URI)") + ctx.handle_connection_loss("Failed to connect to the multiworld server (invalid URI)") except OSError: - ctx._handle_connection_loss("Failed to connect to the multiworld server") + ctx.handle_connection_loss("Failed to connect to the multiworld server") except Exception: - ctx._handle_connection_loss(f"Lost connection to the multiworld server{reconnect_hint()}") + ctx.handle_connection_loss(f"Lost connection to the multiworld server{reconnect_hint()}") finally: await ctx.connection_closed() if ctx.server_address and ctx.username and not ctx.disconnected_intentionally: