Skip to content

Commit

Permalink
fix(client): improve error handling and readability
Browse files Browse the repository at this point in the history
  • Loading branch information
janaab11 committed Jan 4, 2025
1 parent b6d6bc6 commit 74bd40b
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions src/diart/console/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,12 @@ def run():
type=str,
help="Path to an audio file | 'microphone' | 'microphone:<DEVICE_ID>'",
)
parser.add_argument("--host", required=True, type=str, help="Server host")
parser.add_argument("--port", required=True, type=int, help="Server port")
parser.add_argument(
"--host", default="0.0.0.0", type=str, help="Server host. Defaults to 0.0.0.0"
)
parser.add_argument(
"--port", default=7007, type=int, help="Server port. Defaults to 7007"
)
parser.add_argument(
"--step", default=0.5, type=float, help=f"{argdoc.STEP}. Defaults to 0.5"
)
Expand Down Expand Up @@ -93,17 +97,18 @@ def run():

# Wait for READY signal from server
print("Waiting for server to be ready...", end="", flush=True)
while True:
while not stop_event.is_set():
try:
message = ws.recv()
if message.strip() == "READY":
print(" OK")
break
print(f"\nUnexpected message while waiting for READY: {message}")
except WebSocketException as e:
print(f"\nError while waiting for server: {e}")
print(f"\nWebSocket error while waiting for server: {e}")
return

# Start threads for sending and receiving audio
sender = Thread(
target=send_audio,
args=[ws, args.source, args.step, args.sample_rate, stop_event],
Expand All @@ -113,14 +118,25 @@ def run():
sender.start()
receiver.start()

try:
# Wait for threads to complete or for keyboard interrupt
sender.join()
receiver.join()
except KeyboardInterrupt:
print("\nShutting down...")
stop_event.set()

except Exception as e:
print(f"Error: {e}")
stop_event.set()

finally:
stop_event.set()
try:
ws.close()
except:
pass
except WebSocketException:
print("Error closing WebSocket")
except Exception as e:
print(f"Unexpected error closing WebSocket: {e}")


if __name__ == "__main__":
Expand Down

0 comments on commit 74bd40b

Please sign in to comment.