ExerciseOCR/test/test.py
2024-10-25 20:53:51 +08:00

24 lines
790 B
Python

import asyncio
import websockets
async def test_client():
uri = "ws://localhost:8000/event"
async with websockets.connect(uri) as websocket:
print("Connected to server. Waiting for messages...")
try:
while True:
message = await websocket.recv()
print(f"Received message: {message}")
except websockets.exceptions.ConnectionClosed:
print("Connection closed by the server.")
except KeyboardInterrupt:
print("Keyboard interrupt received, closing connection...")
await websocket.close()
print("Connection closed.")
if __name__ == "__main__":
try:
asyncio.run(test_client())
except KeyboardInterrupt:
print("Program terminated by user.")