我想设计我的交易系统,对我订阅的 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;
}
请给我您的建议和其他我可以考虑的想法!提前谢谢!
不会有明显的区别,除非
// signal generation and sending http request to place new orders here
// first before calling async_read() below
都可以
那是因为
async_read
根据定义总是立即返回