Skip to content

Commit

Permalink
Restore websocket.defaulttimeout on connect
Browse files Browse the repository at this point in the history
Fix #413
  • Loading branch information
FeodorFitsner committed Oct 6, 2022
1 parent 2235996 commit 3d72689
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions sdk/python/flet/reconnecting_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ def __init__(self, url) -> None:
self.connected = threading.Event()
self.exit = threading.Event()
self.retry = 0
websocket.setdefaulttimeout(
_LOCAL_CONNECT_TIMEOUT_SEC
if is_localhost_url(url)
else _REMOTE_CONNECT_TIMEOUT_SEC
)
# disable websocket logging completely
# https://github.com/websocket-client/websocket-client/blob/master/websocket/_logging.py#L22-L51
ws_logger = logging.getLogger("websocket")
Expand Down Expand Up @@ -55,6 +50,7 @@ def on_message(self, handler):

def _on_open(self, wsapp) -> None:
logging.info(f"Successfully connected to {self._url}")
websocket.setdefaulttimeout(self.default_timeout)
self.connected.set()
self.retry = 0
if self._on_connect_handler is not None:
Expand All @@ -80,13 +76,18 @@ def close(self) -> None:
self.exit.set()
self.wsapp.close()

# TODO: Can't do CTRL+C while it sleeps between re-connects
# Change to Event: https://stackoverflow.com/questions/5114292/break-interrupt-a-time-sleep-in-python
def _connect_loop(self):
while not self.exit.is_set():
logging.info(f"Connecting Flet Server at {self._url}...")
self.default_timeout = websocket.getdefaulttimeout()
websocket.setdefaulttimeout(
_LOCAL_CONNECT_TIMEOUT_SEC
if is_localhost_url(self._url)
else _REMOTE_CONNECT_TIMEOUT_SEC
)
r = self.wsapp.run_forever()
logging.debug(f"Exited run_forever()")
websocket.setdefaulttimeout(self.default_timeout)
self.connected.clear()
if r != True:
return
Expand Down

0 comments on commit 3d72689

Please sign in to comment.