fix: 按 K-00 约定修复 Python SDK 断线重交与退避

This commit is contained in:
Nixevol
2026-09-30 16:24:13 +08:00
parent 55aa0ccf53
commit 537cef0e32
10 changed files with 564 additions and 126 deletions
+16 -5
View File
@@ -46,13 +46,24 @@ def down_topic(endpoint_id: str) -> str:
return f"nix/c/{endpoint_id}/down"
def normalize_mqtt_ws_url(url: str) -> str:
"""保证 WebSocket 路径以 /mqtt 结尾。"""
def normalize_mqtt_ws_url(url: str, *, allow_tcp: bool = False) -> str:
"""http→ws、https→wss;路径为空或 / 时用 /mqtt,否则保留;mqtt/mqtts 仅显式允许裸 TCP。"""
raw = url.strip()
if "://" not in raw:
raw = "ws://" + raw
u = urlparse(raw)
scheme = u.scheme.lower()
if scheme == "http":
scheme = "ws"
elif scheme == "https":
scheme = "wss"
if scheme in ("mqtt", "mqtts"):
if not allow_tcp:
raise ValueError("裸 TCP 需显式 use_tcp")
return urlunparse((scheme, u.netloc, u.path, u.params, u.query, u.fragment))
if scheme not in ("ws", "wss"):
raise ValueError(f"unsupported scheme {u.scheme}")
path = u.path or ""
if not path.endswith("/mqtt"):
path = path.rstrip("/") + "/mqtt"
return urlunparse((u.scheme, u.netloc, path, u.params, u.query, u.fragment))
if path in ("", "/"):
path = "/mqtt"
return urlunparse((scheme, u.netloc, path, u.params, u.query, u.fragment))