会话保活
WebSocket 连接打开后,仍然需要维持两层状态:
| 层级 | 保活方式 |
|---|---|
| Gateway / REST 会话 | 定期调用 /tickle。 |
| WebSocket 连接 | 定期发送 ech+hb。 |
WebSocket 心跳
Section titled “WebSocket 心跳”官方 release note 中提到,建议每 10 秒发送一次 echo 心跳:
ech+hb收到回声或系统消息后,可以更新连接时间。
REST 会话保活
Section titled “REST 会话保活”curl -k -X POST https://localhost:5000/v1/api/tickle如果 /tickle 返回的认证状态异常,WebSocket 推送也可能很快停止。
import time
def on_open(ws): ws.send("ech+hb")
def heartbeat_loop(ws): while True: ws.send("ech+hb") time.sleep(10)实际项目里要处理异常、退出信号和重连,不要让心跳线程无限残留。
| 现象 | 处理 |
|---|---|
| WebSocket 无消息但未关闭 | 发送 ech+hb 检查后端是否响应。 |
/tickle 失败 | 提示用户重新登录 Gateway。 |
| 频繁断线 | 检查电脑休眠、防火墙、Gateway 会话和网络。 |
| 重连后数据重复 | 重新订阅前先清理订阅状态。 |