流被截断(Boost.Beast / Boost.Asio)

问题描述 投票:0回答:1

问题:

我正在尝试使用 Boost.Beast 编写一个 C++ websocket。与 binary.com API 交互需要 Websocket(不同语言的代码示例 - https://developers.binary.com/demos)。但在“ws.read(buffer);”行我收到错误“流被截断”。

如何重现:

  1. 程序代码 - WebSocket SSL 客户端的 Boost.Beast 示例,同步 - https://github.com/boostorg/beast/blob/develop/example/websocket/client/sync-ssl/websocket_client_sync_ssl.cpp .

  2. 根证书.hpp - 程序代码所需,您可以从这里获取 - https://github.com/boostorg/beast/blob/develop/example/common/root_certificates.hpp .

  3. 不要忘记更改程序代码中的第一行,这一行是

    #include "example/common/root_certificates.hpp
    。如果您将步骤 2 中的 root_certificates.hpp 放置到其他位置,请确保您的代码可以#include 它。

  4. 在步骤 1 的程序代码中放置以下代码:

    argc = 4;

    argv[0] = "websocket-client-sync";

    argv[1] = "ws.binaryws.com";

    argv[2] = "443";

    argv[3] = "{\"ticks\":\"R_100\"}";

    auto target = "/websockets/v3?app_id=1089";
  1. 在步骤 1 的程序代码中,将
    ws.handshake(host, "/");
    替换为
    ws.handshake(host, target);

我想要得到的结果:

您可以查看是否使用此链接 - https://www.websocket.org/echo.html ,在“位置:”中输入

wss://ws.binaryws.com/websockets/v3?app_id=1089
并在“消息:”中输入
{"ticks":"R_100"}
。然后点击“连接”和“发送”。

c++ boost websocket boost-asio boost-beast
1个回答
0
投票

HTTP 1.1 允许在收到最新块时发出“流被截断”消息。所以这取决于服务器,但您的客户端代码(Boost Asio)应该忽略此消息。

例如:

boost::system::error_code error;
boost::asio::read(socket, response, boost::asio::transfer_all(), error);
// Stream truncated is allowed in HTTP 1.1 (and the final chunk is received)
if (error != boost::asio::error::eof && error != boost::asio::ssl::error::stream_truncated)
{
  std::cerr << "Error: Error during reading HTTP response: " << error.message() << std::endl;
}

更多信息:https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Transfer-Encoding

© www.soinside.com 2019 - 2024. All rights reserved.