Skip to content

Commit 05f2df3

Browse files
fix: add warning if websocket connection isnt open (#83)
* fix: add warning if websocket connection isnt open * fix: add warning if websocket connection isnt open * πŸ“ Add docstrings to `fix/ws_open_chck` (#84) * πŸ“ Add docstrings to `fix/ws_open_chck` Docstrings generation was requested by @JarbasAl. * #83 (comment) The following files were modified: * `hivemind_bus_client/client.py` * Update client.py --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: JarbasAI <33701864+JarbasAl@users.noreply.github.com> * chore: avoid infinite recursion --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent b006721 commit 05f2df3

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

β€Žhivemind_bus_client/client.pyβ€Ž

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,11 +233,23 @@ def on_close(self, *args):
233233
self.crypto_key = None
234234
super().on_close(*args)
235235

236-
def wait_for_handshake(self, timeout=5):
236+
def wait_for_handshake(self, timeout=5, max_retries=15):
237+
"""
238+
Waits for the HiveMind handshake to complete; if the handshake is not set and the websocket connection is open, starts the handshake, otherwise waits for the websocket to open and retries.
239+
240+
Parameters:
241+
timeout (float): Number of seconds to wait for each handshake or connection attempt before retrying.
242+
"""
237243
self.handshake_event.wait(timeout=timeout)
238244
if not self.handshake_event.is_set():
239-
self.protocol.start_handshake()
240-
self.wait_for_handshake()
245+
if max_retries <= 0:
246+
raise RuntimeError("timed out waiting for handshake")
247+
if self.connected_event.is_set():
248+
self.protocol.start_handshake()
249+
else:
250+
LOG.warning("Can't start handshake because websocket connection is not yet open...")
251+
self.connected_event.wait(timeout=timeout)
252+
self.wait_for_handshake(timeout, max_retries - 1)
241253

242254
@staticmethod
243255
def build_url(key, host='127.0.0.1', port=5678,
@@ -353,12 +365,12 @@ def emit(self, message: Union[MycroftMessage, HiveMessage],
353365
message = HiveMessage(msg_type=HiveMessageType.BUS,
354366
payload=message)
355367
if not self.connected_event.is_set():
356-
LOG.warning("hivemind connection not ready")
368+
LOG.warning("hivemind connection not ready!")
357369
if not self.connected_event.wait(10):
358370
if not self.started_running:
359371
raise ValueError('You must execute run_forever() '
360372
'before emitting messages')
361-
self.connected_event.wait()
373+
raise RuntimeError(f"Can not send messages before opening the websocket connection. Failed to emit : {message.serialize()}")
362374

363375
try:
364376
# auto inject context for proper routing, this is confusing for

0 commit comments

Comments
Β (0)