对来自 websocket 的每个刻度事件做出反应

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

我想设计我的交易系统,对我订阅的 websocket 流中的每个价格变动事件做出反应。

所以基本上我有两个选择:

void WebsocketClient::on_write(beast::error_code ec,
                               std::size_t bytes_transferred) {
    boost::ignore_unused(bytes_transferred);

    ws_.async_read(buffer_,
                   beast::bind_front_handler(&WebsocketClient::on_message,
                                             shared_from_this()));
}

void WebsocketClient::on_message(beast::error_code ec,
                                 std::size_t bytes_transferred) {

    // signal generation and sending http request to place new orders here
    // first before calling async_read() below

    std::cout << "Received: " << beast::buffers_to_string(buffer_.cdata())
              << std::endl;

    ws_.async_read(buffer_,
                   beast::bind_front_handler(&WebsocketClient::on_message,
                                             shared_from_this()));
}

或者我可以

void WebsocketClient::on_message(beast::error_code ec,
                            std::size_t bytes_transferred) {
    ws_.async_read(buffer_,
                   beast::bind_front_handler(&WebsocketClient::on_message,
                                             shared_from_this()));

    // signal generation and sending http request to place new orders here after
    // calling async_read()

    std::cout << "Received: " << beast::buffers_to_string(buffer_.cdata())
              << std::endl;
}

请给我您的建议和其他我可以考虑的想法!提前谢谢!

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

不会有明显的区别,除非

// signal generation and sending http request to place new orders here
// first before calling async_read() below

都可以

  • 需要大量时间(IO 线程上的设计味道)
  • 通过返回/异常退出函数

那是因为

async_read
根据定义总是立即返回

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