-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
38 lines (33 loc) · 1.25 KB
/
Copy pathtest.py
File metadata and controls
38 lines (33 loc) · 1.25 KB
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
37
38
import asyncio
import websockets
import json
import time
async def websocket_client():
uri = "ws://localhost:9002"
async with websockets.connect(uri) as websocket:
# 首次连接认证
auth = json.dumps({"action": "auth", "token": "your_token"}, ensure_ascii=False)
await websocket.send(auth)
i = 0
while True:
i += 1
await asyncio.sleep(1)
print(f"发送消息 {i}...")
# 构造中文消息(确保不转码)
msg = json.dumps({
"seq": i,
"time": time.strftime("%H:%M:%S"),
}, ensure_ascii=False)
await websocket.send(msg)
print(f"📤 发送: {msg}")
try:
response = await asyncio.wait_for(websocket.recv(), timeout=1)
try:
# 尝试解析并美化打印JSON
parsed = json.loads(response)
print(f"📥 收到: {json.dumps(parsed, indent=2, ensure_ascii=False)}")
except:
print(f"📥 收到: {response}")
except asyncio.TimeoutError:
print("⏱️ 响应超时")
asyncio.run(websocket_client())