import json, websocket, urllib.request

req = urllib.request.Request("http://127.0.0.1:8081/api/login",
    data=json.dumps({"username":"cxbsoft","password":"cf5584aca177a811ffda8f4789e2e143"}).encode(),
    headers={"Content-Type":"application/json"})
token = json.load(urllib.request.urlopen(req, timeout=10))["data"]["token"]

ws = websocket.create_connection(f"ws://127.0.0.1:8081/api/user/friend-agent/ws?token={token}", timeout=30)
# 读前几条消息看是否有 waking 事件
import select
for i in range(3):
    try:
        msg = ws.recv()
        print(f"msg{i}:", msg[:300])
    except Exception as e:
        print(f"msg{i} error:", e)
        break
