oclaw/interfaces/ws/runtime_loop.py
oliver 7253f6795d 完成 WebSocket 与 workspace 架构优化收尾,并修复全量回归阻塞。
本次将握手鉴权、Origin 校验、限流、重连补偿、发送背压与观测字段打通,同时修复 gateway 异常路径与备份清理兼容问题,确保工具历史压缩语义一致并恢复全量测试通过。

Made-with: Cursor
2026-04-27 20:41:50 +08:00

46 lines
1.5 KiB
Python

from __future__ import annotations
from typing import Any
async def close_ws(conn: Any, code: int = 1000, reason: str = "done") -> None:
try:
await conn.ws.close(code=code, reason=reason)
except Exception:
pass
async def run_connection_loop(conn: Any) -> None:
if hasattr(conn, "validate_origin") and not conn.validate_origin():
await close_ws(conn, 1008, "origin not allowed")
return
await conn.ws.accept()
await conn.send_event("connect.challenge", {"nonce": conn.connect_nonce, "ts": conn._now_ms()})
while True:
frame = await conn._recv_frame(preauth=not conn.connected)
if frame is None:
return
if frame.get("type") != "req":
await conn.send_res(
str(frame.get("id") or "invalid"),
ok=False,
error=conn._error_shape("INVALID_REQUEST", "expected req frame"),
)
if not conn.connected:
await close_ws(conn, 1008, "invalid handshake")
return
continue
req_id = str(frame.get("id") or "invalid")
method = str(frame.get("method") or "")
params = frame.get("params")
if not conn.connected:
await conn._handle_connect(req_id, method, params)
if conn.handshake_failed:
await close_ws(conn, 1008, "invalid handshake")
return
continue
await conn._dispatch_connected(req_id, method, params)
__all__ = ["run_connection_loop", "close_ws"]