Skip to content

TLSStreamProtocolError #177

@geohuz

Description

@geohuz
import pkg/[chronos, chronicles, stew/byteutils]
import websock/websock
import std/[strformat, json]

const
  clientFlags = {NoVerifyHost}
  host = "api.hyperliquid.xyz"
  port = 443
  path = "/ws"
  agent = "nim-ws-client"

logScope:
  topics = "hyperliquid-ws"

# 封装连接
proc connectServer(path: string): Future[WSSession] {.async.} =
  let ws = await WebSocket.connect(
    host = &"{host}:{port}",  # 官方例子也用 host:port 方式
    path = path,
    secure = true,
    flags = clientFlags
  )
  return ws

proc main() {.async.} =
  info "Connecting to Hyperliquid WebSocket...", host=host, port=port, path=path

  let ws = await connectServer(path)
  info "✅ Connected", state = ws.readyState

  # 发送合法 JSON 消息
  let msgObj = %*{
    "channel": "heartbeat",
    "type": "ping",
    "data": {}
  }
  let msgStr = $msgObj
  await ws.send(msgStr)
  trace "Sent JSON message", data=msgStr

  # 接收服务器响应
  let buff = await ws.recvMsg()
  if buff.len > 0:
    let resp = string.fromBytes(buff)
    info "Received response", resp=resp
  else:
    warn "Empty response from server"

  # 关闭连接
  await ws.close()
  info "Closed connection"

when isMainModule:
  waitFor main()

Getting:

INF 2025-10-20 17:32:39.158+00:00 Connecting to Hyperliquid WebSocket... topics="hyperliquid-ws" tid=4025441 host=api.hyperliquid.xyz port=443 path=/ws asyncstream.nim(814) write client.nim(108) request websock.nim(157) connect websock.nim(161) connect ??? connect main.nim(17) connectServer main.nim(28) main asyncfutures.nim(649) waitFor Error: unhandled exception: (RecvFatalAlert) Fatal alert (40) received from the peer (code: 296) [TLSStreamProtocolError]

I've tried with a nodejs client without any issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions