From b194b3c42a05c5f4f40f2494722e8a10898ad7c8 Mon Sep 17 00:00:00 2001 From: Dominick Meglio Date: Sun, 25 Dec 2022 23:26:30 -0500 Subject: [PATCH] Updated websocket library and fixed bugs due to reconnects --- requirements.txt | 2 +- .../websockets/websocket_transport.py | 31 +++++++------------ 2 files changed, 13 insertions(+), 20 deletions(-) diff --git a/requirements.txt b/requirements.txt index 1a456d7..2e0679f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ msgpack==1.0.2 requests>=2.22.0 -websocket-client==1.0.0 +websocket-client==1.4.2 diff --git a/signalrcore/transport/websockets/websocket_transport.py b/signalrcore/transport/websockets/websocket_transport.py index 283d921..1c0c3cf 100644 --- a/signalrcore/transport/websockets/websocket_transport.py +++ b/signalrcore/transport/websockets/websocket_transport.py @@ -7,9 +7,8 @@ from .reconnection import ConnectionStateChecker from .connection import ConnectionState from ...messages.ping_message import PingMessage -from ...hub.errors import HubError, HubConnectionError, UnAuthorizedHubError +from ...hub.errors import HubError, UnAuthorizedHubError from ...protocol.messagepack_protocol import MessagePackHubProtocol -from ...protocol.json_hub_protocol import JsonHubProtocol from ..base_transport import BaseTransport from ...helpers import Helpers @@ -26,7 +25,6 @@ def __init__(self, super(WebsocketTransport, self).__init__(**kwargs) self._ws = None self.enable_trace = enable_trace - self._thread = None self.skip_negotiation = skip_negotiation self.url = url if headers is None: @@ -37,7 +35,6 @@ def __init__(self, self.token = None # auth self.state = ConnectionState.disconnected self.connection_alive = False - self._thread = None self._ws = None self.verify_ssl = verify_ssl self.connection_checker = ConnectionStateChecker( @@ -48,16 +45,16 @@ def __init__(self, if len(self.logger.handlers) > 0: websocket.enableTrace(self.enable_trace, self.logger.handlers[0]) - + def is_running(self): return self.state != ConnectionState.disconnected def stop(self): + self.connection_checker.stop() if self.state == ConnectionState.connected: - self.connection_checker.stop() self._ws.close() - self.state = ConnectionState.disconnected - self.handshake_received = False + self.state = ConnectionState.disconnected + self.handshake_received = False def start(self): if not self.skip_negotiation: @@ -69,7 +66,7 @@ def start(self): self.state = ConnectionState.connecting self.logger.debug("start url:" + self.url) - + self._ws = websocket.WebSocketApp( self.url, header=self.headers, @@ -77,15 +74,11 @@ def start(self): on_error=self.on_socket_error, on_close=self.on_close, on_open=self.on_open, - ) - - self._thread = threading.Thread( - target=lambda: self._ws.run_forever( - sslopt={"cert_reqs": ssl.CERT_NONE} - if not self.verify_ssl else {} - )) - self._thread.daemon = True - self._thread.start() + ) + + self._ws.run_forever( + sslopt={"cert_reqs": ssl.CERT_NONE} if not self.verify_ssl else {}, + ) return True def negotiate(self): @@ -185,7 +178,7 @@ def on_message(self, app, raw_message): return self._on_message(messages) return [] - + return self._on_message( self.protocol.parse_messages(raw_message))