You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Boost WebSocket握手被远端拒绝 连接Kraken WebSocket API失败问题咨询

问题根因排查

你遇到的握手被拒绝的核心原因是WebSocket SSL握手阶段缺失SNI配置,具体问题点和修复方案如下:

  • 核心问题:init_webSocket方法中,完成TCP连接后、SSL握手前,没有调用SSL_set_tlsext_host_name为SSL上下文设置SNI扩展。Kraken的服务端集群依赖SNI路由请求,缺失该扩展会直接被拒绝握手;Binance服务端对SNI要求宽松,因此之前对接正常。
  • 次要问题:主函数循环中读取数据后直接return 1,缓冲区清理逻辑永远不会执行,但该问题不影响握手阶段。

修复方案

修改init_webSocket方法,在SSL握手前补充SNI设置,参考代码如下:

void init_webSocket(std::string const& host, std::string const& port, std::string const& target) {
    auto const results = resolver_webSocket.resolve(host, port);
    net::connect(ws.next_layer().next_layer(), results.begin(), results.end());
    // 新增:设置SNI扩展,和init_http逻辑保持一致
    if (!SSL_set_tlsext_host_name(ws.next_layer().native_handle(), host.c_str()))
    {
        boost::system::error_code ec{ static_cast<int>(::ERR_get_error()), boost::asio::error::get_ssl_category() };
        throw boost::system::system_error{ ec };
    }
    ws.next_layer().handshake(ssl::stream_base::client);
    // Set a decorator to change the User-Agent of the handshake
    ws.set_option(websocket::stream_base::decorator(
        [](websocket::request_type& req)
    {
        req.set(http::field::user_agent,
            std::string(BOOST_BEAST_VERSION_STRING) +
            " websocket-client-coro");
    }));
    ws.handshake(host, target.c_str());
}

如果修复后仍有异常,可以尝试将ctx_webSocket的TLS版本改为tlsv12_client验证兼容性。

内容的提问来源于stack exchange,提问作者mathew sada

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 05:54:03