QDAirPortBackend0122/tools/test_adxp_websocket_direct.py
2026-01-22 13:19:47 +08:00

86 lines
2.5 KiB
Python
Raw Permalink 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.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
ADXP WebSocket连接测试脚本
直接测试WebSocket连接不进行登录认证
"""
import json
import time
try:
# 尝试导入websocket-client库
from websocket import WebSocketApp
print("✅ 成功导入websocket库")
except ImportError:
try:
# 备用导入方式
import websocket
from websocket import WebSocketApp
print("✅ 通过备用方式导入websocket库")
except ImportError:
print("❌ 未找到websocket库请安装: pip install websocket-client")
exit(1)
def on_message(ws, message):
"""处理接收到的消息"""
print(f"📥 收到消息: {message[:100]}...")
try:
# 尝试解析JSON消息
data = json.loads(message)
if isinstance(data, list):
print(f" 解析到 {len(data)} 条航班消息")
for i, msg in enumerate(data):
service_code = msg.get('serviceCode', 'N/A')
print(f" [{i+1}] 服务代码: {service_code}")
# 显示部分消息内容
content = msg.get('content', '')
if content:
print(f" 内容预览: {content[:50]}...")
else:
print(f" 消息内容: {data}")
except json.JSONDecodeError:
print(f" 非JSON消息: {message}")
def on_error(ws, error):
"""处理错误"""
print(f"❌ WebSocket错误: {error}")
def on_close(ws, close_status_code, close_msg):
"""处理连接关闭"""
print(f"🔒 连接已关闭: 状态码={close_status_code}, 消息={close_msg}")
def on_open(ws):
"""处理连接打开"""
print("✅ WebSocket连接已建立")
print("🚀 开始监听消息...")
def main():
"""主函数"""
# WebSocket URL
ws_url = "ws://localhost:8086/ws/flight-notifications"
print(f"🚀 正在连接到ADXP适配器WebSocket服务: {ws_url}")
# 创建WebSocket连接
ws = WebSocketApp(ws_url,
on_open=on_open,
on_message=on_message,
on_error=on_error,
on_close=on_close)
try:
# 启动连接(阻塞)
ws.run_forever()
except KeyboardInterrupt:
print("\n⚠️ 用户中断")
ws.close()
except Exception as e:
print(f"❌ 测试过程中发生错误: {e}")
import traceback
traceback.print_exc()
ws.close()
if __name__ == "__main__":
main()