跳转至

WebSocket 连接

Base

WebSocket 主机与 REST API 主机不同(见 Base URLs):

环境 Base
生产 wss://ws.pcrypto.com

下方示例用 <websocket-host> 作占位符 — 请按 Base URLs 换成你所在环境的主机。

公开行情

Path 行为
/openapi/quote/ws/v1/<streamName> 单流 — 连接时已绑定;无需 SUBSCRIBE。若需动态增删流,请使用组合模式。
/openapi/quote/stream?streams=a/b/c 组合模式;每条消息包装为 { "stream", "data" }

示例:

wss://<websocket-host>/openapi/quote/ws/v1/btcusdt@aggTrade
wss://<websocket-host>/openapi/quote/stream?streams=btcusdt@aggTrade/btcusdt@depth20

组合模式载荷示例:

{
  "stream": "btcusdt@aggTrade",
  "data": {
    "e": "aggTrade",
    "E": 1672515782136,
    "s": "BTCUSDT",
    "a": 5933014,
    "p": "0.001",
    "q": "100",
    "f": 100,
    "l": 105,
    "T": 1672515782136,
    "m": true
  }
}

data 与该频道单流载荷一致。

私有用户数据

wss://<websocket-host>/openapi/ws/spot

握手后发送鉴权消息(listenKey 放在消息体中,不要放进 URL):

{ "op": "auth", "listenKey": "<listenKey>" }

通过 REST 创建 / 续期 / 关闭 listenKey(/openapi/v1/userDataStream,仅 API Key)。事件字段与枚举见 私有用户数据。

不支持的路径

请勿使用以下路径:

Path 说明
/ws、/ws/spot 遗留别名 — 请改用 /openapi/quote/… 与 /openapi/ws/…

心跳

方向 机制
服务端 → 客户端 WebSocket Ping 每 20s;须在 120s 内 Pong,否则断开
客户端 → 服务端 JSON {"ping": <epochMs>} → {"pong": <epochMs>}

优先应答 WebSocket Ping 帧。JSON ping/pong 为兼容回退。

限制

限制 值
客户端消息 25 / s
空闲公共连接 公共连接在零订阅时 60s 后断开。私有 listenKey 会话无需 SUBSCRIBE。

订阅 / 退订

{ "method": "SUBSCRIBE", "params": ["btcusdt@aggTrade"], "id": 1 }
{ "method": "UNSUBSCRIBE", "params": ["btcusdt@aggTrade"], "id": 2 }
{ "method": "LIST_SUBSCRIPTIONS", "id": 3 }

成功回复形如:{ "result": null, "id": <n> }(LIST 时为订阅列表)。

亦接受遗留 op 形式;优先使用 method / params:

遗留 推荐
"op": "subscribe" "method": "SUBSCRIBE"
"args": [...] "params": [...]