I have a Websocket class that connects asynchronously using Boost.Asio and Boost.Beast. The following code was working fine with Boost 1.78, but after updating to the latest Boost version, I now get a compile error:
std::future<void> Websocket::async_connect() {
io_thread_ = std::thread([this]() {
ioc_.run();
});
// Start resolve
auto resolve_fut = resolver_.async_resolve(host_, port_, net::use_future);
return std::async(std::launch::async, [this, resolve_fut = std::move(resolve_fut)]() mutable {
try {
auto results = resolve_fut.get();
auto connect_fut = beast::get_lowest_layer(ws_).async_connect(*results, net::use_future);
connect_fut.get();
auto ssl_handshake_fut = ws_.next_layer().async_handshake(ssl::stream_base::client, net::use_future);
ssl_handshake_fut.get();
ws_.set_option(websocket::stream_base::timeout::suggested(beast::role_type::client));
auto ws_handshake_fut = ws_.async_handshake(host_, target_, net::use_future);
ws_handshake_fut.get();
webSocketLogger_.info("Connected to WebSocket Successfully!!!");
// std::cout << "Connected to WebSocket Successfully!!!\n";
ReceiveMsg();
} catch (const std::exception& e) {
// std::cerr << "Connection failed: " << e.what() << "\n";
webSocketLogger_.error("Connection Failed: {}", e.what());
throw;
}
The error I get is:
No operator* matches argument of type
std::tuple<boost::system::error_code, boost::asio::ip::basic_resolver_results<tcp>>
In Boost 1.78, resolve_fut.get() returned a type that could be directly dereferenced to get tcp::endpoint results. In the latest Boost, it seems async_resolve(..., net::use_future) now returns a tuple instead of a single object.
I have tried things like:
auto endpoints = std::get<1>(results);
auto [err,endpoints] = resolve_fut.get();
but it still doesn’t compile.
Question:
How should I update this code to work with the latest Boost version? What is the correct way to extract endpoints from the future returned by async_resolve?