0

下面的代码失败并出现错误

terminate called after throwing an instance of 'boost::wrapexcept<boost::system::system_error>'
  what():  The WebSocket handshake was declined by the remote peer

第一次握手工作正常,写入工作正常,但在阅读时我收到此错误,非常感谢任何线索/帮助。提前致谢:

namespace net       = boost::asio;
namespace ssl       = net::ssl;
namespace beast     = boost::beast;
namespace http      = beast::http;
namespace websocket = beast::websocket;
using tcp = net::ip::tcp;
using stream_t = websocket::stream<ssl::stream<tcp::socket>>;

int test() {

    std::string host = "vstream.binance.com";
    auto const port = "443";
    auto const path  = "/ws/BTC-211112-59000-P@depth20";
    auto const rpcJson = "{\"method\":\"BINARY\", \"params\":[\"false\"], \"id\":1}";

    net::io_context ioc;
    tcp::resolver resolver{ ioc };

    ssl::context ctx { ssl::context::sslv23 };
    ctx.set_verify_mode(ssl::verify_none);

    stream_t s{ ioc, ctx };
    net::connect(beast::get_lowest_layer(s), resolver.resolve(host, port));

    // SSL handshake
    s.next_layer().handshake(ssl::stream_base::client);

    s.handshake(host + ":" + port, path);

    std::cout << "connected." << std::endl;

    // send request to the websocket
    s.write(net::buffer("{\"method\":\"BINARY\", \"params\":[\"false\"], \"id\":1}"));

    {
        boost::beast::multi_buffer m_buf;
        s.read(m_buf);
        auto size = m_buf.size();
        std::string strbuf;
        strbuf.reserve(size);

        for ( const auto &it: m_buf.data() ) {
            strbuf.append(static_cast<const char *>(it.data()), it.size());
        }
        m_buf.consume(m_buf.size());

        std::cout << "s. " << strbuf << std::endl;
    }
    return 0;
}

我的主要困惑是为什么最初的握手没有失败以及导致它在读取时失败的原因。

4

0 回答 0