/* Copyright (c) 2018-2022 Marcelo Zimbres Silva (mzimbres@gmail.com) * * Distributed under the Boost Software License, Version 1.0. (See * accompanying file LICENSE.txt) */ #include #if defined(BOOST_ASIO_HAS_CO_AWAIT) #include #include #include "common.hpp" namespace net = boost::asio; using namespace net::experimental::awaitable_operators; using resolver = net::use_awaitable_t<>::as_default_on_t; using signal_set_type = net::use_awaitable_t<>::as_default_on_t; using timer_type = net::use_awaitable_t<>::as_default_on_t; using aedis::adapt; using aedis::resp3::request; using aedis::resp3::node; /* This example will subscribe and read pushes indefinitely. * * To test send messages with redis-cli * * $ redis-cli -3 * 127.0.0.1:6379> PUBLISH channel some-message * (integer) 3 * 127.0.0.1:6379> * * To test reconnection try, for example, to close all clients currently * connected to the Redis instance * * $ redis-cli * > CLIENT kill TYPE pubsub */ // Receives pushes. auto receiver(std::shared_ptr conn) -> net::awaitable { for (std::vector> resp;;) { co_await conn->async_receive(adapt(resp)); std::cout << resp.at(1).value << " " << resp.at(2).value << " " << resp.at(3).value << std::endl; resp.clear(); } } auto async_main() -> net::awaitable { auto ex = co_await net::this_coro::executor; auto conn = std::make_shared(ex); signal_set_type sig{ex, SIGINT, SIGTERM}; timer_type timer{ex}; request req; req.get_config().cancel_on_connection_lost = true; req.push("HELLO", 3); req.push("SUBSCRIBE", "channel"); // The loop will reconnect on connection lost. To exit type Ctrl-C twice. for (;;) { co_await connect(conn, "127.0.0.1", "6379"); co_await ((conn->async_run() || healthy_checker(conn) || sig.async_wait() || receiver(conn)) && conn->async_exec(req)); conn->reset_stream(); timer.expires_after(std::chrono::seconds{1}); co_await timer.async_wait(); } } #endif // defined(BOOST_ASIO_HAS_CO_AWAIT)