Stream truncated (Boost.Beast / Boost.Asio)

2.7k views Asked by At

Problem:

I'm trying to write a C++ websocket with Boost.Beast. Websocket is needed for interacting with binary.com API (code samples in different languages - https://developers.binary.com/demos). But at line "ws.read(buffer);" I get an error "stream truncated".

How to reproduce:

  1. Program code - Boost.Beast example of WebSocket SSL client, synchronous - https://github.com/boostorg/beast/blob/develop/example/websocket/client/sync-ssl/websocket_client_sync_ssl.cpp .

  2. Root certificates.hpp - needed for program code, you can take it from here - https://github.com/boostorg/beast/blob/develop/example/common/root_certificates.hpp .

  3. Don't forget to change the top line in program code, this line is #include "example/common/root_certificates.hpp. If you placed your root_certificates.hpp from step 2 to another place, make sure your code can #include it.

  4. In program code from step 1 place this code:

    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. In program code from step 1 replace ws.handshake(host, "/"); on ws.handshake(host, target);

What I'm trying to get as result:

You can see if you go with this link - https://www.websocket.org/echo.html , in "Location:" type wss://ws.binaryws.com/websockets/v3?app_id=1089 and in "Message:" type {"ticks":"R_100"} . And then click "Connect" and "Send".

1

There are 1 answers

0
Melroy van den Berg On

HTTP 1.1 is allowed to turn a "stream truncated" message when the latest chunk has been received. So it depends on the server, but your client code (Boost Asio) should just ignore this message.

For example:

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;
}

More info: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Transfer-Encoding