-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_websocket.py
More file actions
36 lines (26 loc) · 956 Bytes
/
test_websocket.py
File metadata and controls
36 lines (26 loc) · 956 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import asyncio
from maxhttp import WebSocket
async def websocket_client():
url = "wss://echo.websocket.org"
async with WebSocket.connect(url) as ws:
# Send text message
message = "Hello from maxhttp 👋"
await ws.send_text(message)
print("Sent:", message)
# Receive text response
reply = await ws.recv()
print("Received text:", reply)
# Send binary payload
payload = b"\x00\x01binary-data"
await ws.send_bytes(payload)
print("Sent binary:", payload)
received = await ws.recv()
print("Received binary:", received)
# Send JSON payload
json_payload = {"type": "ping", "message": "hello"}
await ws.send_json(json_payload)
print("Sent JSON:", json_payload)
reply_json = await ws.recv_json()
print("Received JSON:", reply_json)
if __name__ == "__main__":
asyncio.run(websocket_client())