BodyBalanceEvaluation/test_websocket.py
2025-07-29 18:28:40 +08:00

53 lines
1.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import socketio
import time
# 创建SocketIO客户端
sio = socketio.SimpleClient()
print('连接WebSocket并监听事件...')
try:
# 连接到服务器
print('正在连接到192.168.1.173:5000...')
sio.connect('http://192.168.1.173:5000', wait_timeout=10)
print('WebSocket连接成功!')
# 发送启动RTSP事件
sio.emit('start_rtsp')
print('已发送start_rtsp事件等待5秒接收数据...')
# 等待并接收事件
for i in range(5):
try:
# 接收事件
event = sio.receive(timeout=1)
if event:
event_name, data = event
print(f'收到事件: {event_name}, 数据类型: {type(data)}')
if event_name == 'rtsp_frame' and isinstance(data, dict) and 'image' in data:
print(f'收到图像数据,长度: {len(data["image"])} 字符')
elif event_name == 'rtsp_status':
print(f'RTSP状态: {data}')
except socketio.exceptions.TimeoutError:
print(f'等待事件超时 ({i+1}/5)')
# 发送停止RTSP事件
sio.emit('stop_rtsp')
print('已发送stop_rtsp事件')
# 等待停止状态事件
try:
event = sio.receive(timeout=2)
if event:
event_name, data = event
print(f'收到停止事件: {event_name}, 数据: {data}')
except socketio.exceptions.TimeoutError:
print('等待停止事件超时')
sio.disconnect()
print('WebSocket连接已断开')
except Exception as e:
print(f'测试过程中发生错误: {e}')
print('WebSocket测试完成')