This commit is contained in:
薇薇安 2026-01-14 13:29:35 +08:00
parent aa1b9065d8
commit 0fc718dbe3
2 changed files with 175 additions and 6 deletions

View File

@ -0,0 +1,143 @@
# WebSocket 故障排除指南
## 常见错误及解决方案
### 错误1: `__init__() got an unexpected keyword argument 'throw_exception_if_unrepairable'`
**原因**:
- `unicorn-binance-websocket-api` 2.4.0 版本不支持 `throw_exception_if_unrepairable` 参数
- 该参数可能在新版本中被移除或改名
**解决方案**:
- ✅ 已修复:移除了不支持的参数
- ✅ 已添加:多种初始化方式的回退机制
- ✅ 已优化:更好的错误日志输出
**修复后的行为**:
1. 首先尝试使用 `high_performance=True` 参数
2. 如果失败,尝试只使用 `exchange` 参数
3. 如果还是失败,尝试使用位置参数
4. 所有方式都失败时,输出详细的错误信息
### 错误2: `ModuleNotFoundError: No module named 'unicorn_binance_websocket_api'`
**原因**:
- 未安装 `unicorn-binance-websocket-api` 依赖
**解决方案**:
```bash
cd trading_system
pip install -r requirements.txt
# 或单独安装
pip install unicorn-binance-websocket-api==2.4.0
```
### 错误3: WebSocket连接失败
**可能原因**:
1. 网络连接问题
2. 防火墙阻止
3. Binance服务器问题
**检查步骤**:
```bash
# 检查网络连接
ping stream.binance.com
# 检查DNS解析
nslookup stream.binance.com
```
**解决方案**:
- 检查服务器网络配置
- 检查防火墙规则
- 等待Binance服务恢复
### 错误4: WebSocket订阅失败
**可能原因**:
1. 订阅的交易对数量过多
2. 交易对格式错误
3. WebSocket连接不稳定
**解决方案**:
- 减少订阅数量(调整 `WEBSOCKET_SUBSCRIBE_COUNT`
- 检查交易对格式(应该是大写,如 `BTCUSDT`
- 查看日志中的详细错误信息
## 验证WebSocket是否正常工作
### 1. 查看启动日志
正常启动应该看到:
```
✓ Unicorn WebSocket管理器启动成功 (测试网: False)
交易所: binance.com-futures
✓ 已成功订阅 100 个交易对的实时价格流
✓ 价格数据将通过WebSocket实时更新减少REST API调用
```
### 2. 查看运行时日志
正常运行时应该看到:
```
✓ [WebSocket] 从缓存获取 BTCUSDT 价格: 43250.50000000 (缓存年龄: 2.3秒)
```
### 3. 查看统计信息
每次扫描后应该看到:
```
WebSocket状态: 订阅=100个交易对, 缓存=100个价格, 活跃流=1个
```
## 调试技巧
### 启用详细日志
`config.py` 或环境变量中设置:
```python
LOG_LEVEL = 'DEBUG'
```
### 检查WebSocket状态
在代码中添加:
```python
if client.unicorn_manager:
stats = client.unicorn_manager.get_stream_statistics()
print(f"WebSocket统计: {stats}")
```
### 手动测试WebSocket连接
```python
from unicorn_binance_websocket_api.manager import BinanceWebSocketApiManager
manager = BinanceWebSocketApiManager(exchange="binance.com-futures")
stream_id = manager.create_stream(["arr"], ["btcusdt@ticker"])
print(f"Stream ID: {stream_id}")
```
## 版本兼容性
### 支持的版本
- `unicorn-binance-websocket-api >= 2.0.0`
- 推荐版本: `2.4.0`
### 已知问题
- 2.4.0版本不支持 `throw_exception_if_unrepairable` 参数(已修复)
- 某些旧版本可能不支持 `high_performance` 参数(已添加回退)
## 性能优化建议
1. **订阅数量**: 根据实际需求调整,不要订阅过多
2. **缓存TTL**: 默认60秒可以根据需要调整
3. **日志级别**: 生产环境使用 `INFO`,调试时使用 `DEBUG`
## 联系支持
如果问题仍然存在:
1. 查看完整的错误日志
2. 检查 `unicorn-binance-websocket-api` 版本
3. 查看 [Unicorn Binance WebSocket API 文档](https://github.com/LUCIT-Systems-and-Development/unicorn-binance-websocket-api)

View File

@ -48,17 +48,43 @@ class UnicornWebSocketManager:
"""启动WebSocket管理器"""
try:
# 创建管理器
# 注意unicorn-binance-websocket-api 2.4.0版本不支持某些参数
exchange = "binance.com-futures" if not self.testnet else "binance.com-futures-testnet"
# 尝试不同的初始化方式(兼容不同版本)
try:
# 方式1使用high_performance参数如果支持
self.manager = BinanceWebSocketApiManager(
exchange="binance.com-futures" if not self.testnet else "binance.com-futures-testnet",
throw_exception_if_unrepairable=True,
exchange=exchange,
high_performance=True
)
logger.debug("使用 high_performance=True 参数初始化成功")
except TypeError as e1:
# 方式2只使用exchange参数
try:
self.manager = BinanceWebSocketApiManager(exchange=exchange)
logger.debug("使用 exchange 参数初始化成功")
except TypeError as e2:
# 方式3使用位置参数
try:
self.manager = BinanceWebSocketApiManager(exchange)
logger.debug("使用位置参数初始化成功")
except Exception as e3:
logger.error(f"所有初始化方式都失败:")
logger.error(f" 方式1错误: {e1}")
logger.error(f" 方式2错误: {e2}")
logger.error(f" 方式3错误: {e3}")
raise e3
self.running = True
logger.info(f"Unicorn WebSocket管理器启动成功 (测试网: {self.testnet})")
logger.info(f"✓ Unicorn WebSocket管理器启动成功 (测试网: {self.testnet})")
logger.info(f" 交易所: {exchange}")
return True
except Exception as e:
logger.error(f"启动Unicorn WebSocket管理器失败: {e}")
logger.error(f"错误类型: {type(e).__name__}")
import traceback
logger.debug(f"详细错误信息:\n{traceback.format_exc()}")
return False
def stop(self):