Skip to content
dsh-invest 文档
Esc
↑↓navigate↵open⌘Jpreview
On this page

实时流

实现 stream 切面:连接、订阅、帧与重连边界。

自选状态行:实时、延迟、过期。

自选状态行:实时、延迟、过期。

目标

让自选、图表和盘口收到实时推送。provider 只描述「怎么连、怎么订阅」,连接池、重连、退避、代理和 topic 账本都在宿主的 Hub 里。

要改哪些文件

src/shared/manifest.ts   给 facets.stream 填 caps 与 connect
src/host/stream.ts       connect(init) → ConnectionDriver
src/host/stream-driver.ts 上游协议:订阅帧、解帧、心跳
tests/stream.spec.ts     用假 socket 钉住 connKey、退订与错误帧

契约

stream: {
  caps: {
    channels: ['ticker', 'trades'],
    markets: ['spot'],
    timeframes: ['1m'],
    unwatch: true,
    push: true,
    connKey: (spec) => `demo#public`,
  },
  async connect(init) {
    const socket = await init.openSocket('wss://example.invalid/ws');
    return {
      ready: openOnce(socket),
      subscribe(spec) { socket.send(JSON.stringify({ op: 'sub', channel: spec.channel, symbol: spec.series.symbol })); },
      unsubscribe(spec) { socket.send(JSON.stringify({ op: 'unsub', channel: spec.channel, symbol: spec.series.symbol })); },
      frames: framesOf(socket, init.specs),
      async close() { socket.close(); },
    };
  },
}

规则:

  • caps.channels 至少一个;caps.markets 缺省 = manifest 全部市场。只在部分市场存在的通道用 channelMarkets 收窄。
  • connKey 决定哪些订阅共用一条物理连接。能合并的返回 ${venue}#public,不能的返回 ${venue}#${symbol}。必须非空。
  • unwatch 必须声明;false 时 Hub 不调 unsubscribe,只能关连接退订。
  • push: true 表示真推送。轮询型驱动声明 true 会把一条批量请求放大成 N 条单标的请求。
  • 帧必须把 key 原样带回;kind: 'status' 用于报告 live / retrying。
  • 私有通道(balance / orders / positions / myTrades)用 spec.credentialRef 取凭据,不要自己按工作区猜。

失败与重试

  • ready 解析 = 连上且首批订阅被上游确认。握手失败时 reject,Hub 决定重连。
  • frames 结束(上游断开)由 Hub 决定「重连还是放弃」。provider 不要在驱动里写自己的重连循环。
  • 不支持的 spec 在订阅入口拒绝(not-covered),不要让一条坏 spec 把同一连接上的其它订阅一起拖垮。
  • 断线期间的状态要能说清:status 帧写 retrying,恢复后写 live;界面据此显示「实时不可用 / 已恢复实时」。

验证

curl -s http://127.0.0.1:<port>/dsh-invest/health | jq '.invest.facets.stream'

预期比安装前增加 1。

pnpm --filter dsh-invest-provider-demo exec vitest run tests/conformance.spec.ts -t stream

预期通过;describeVenue 会逐个 channel × market 调 connKey,返回空串或抛错都会失败。

打开自选或图表,观察 ticker 通道;状态行显示 实时不可用 时先看代理与上游,不要反复刷新页面。

Was this page helpful?