larra/examples/src/connect.cpp
Ivan-lis c52d237dc4
All checks were successful
PR Check / on-push-commit-check (push) Successful in 13m52s
Feature: iq::Roster
2024-11-25 13:20:32 +00:00

58 lines
1.9 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <spdlog/common.h>
#include <spdlog/spdlog.h>
#include <boost/asio/co_spawn.hpp>
#include <boost/asio/detached.hpp>
#include <larra/client/client.hpp>
#include <larra/presence.hpp>
#include <larra/printer_stream.hpp>
#include <print>
namespace iq = larra::xmpp::iq;
auto Coroutine() -> boost::asio::awaitable<void> {
SPDLOG_INFO("Connecting client...");
try {
auto client = co_await larra::xmpp::client::CreateClient<larra::xmpp::PrintStream<boost::asio::ip::tcp::socket>>(
larra::xmpp::PlainUserAccount{.jid = {.username = "test1", .server = "localhost"}, .password = "test1"},
{.useTls = larra::xmpp::client::Options::kNever});
// rfc6120 7.1
// After a client authenticates with a server,
// it MUST bind a specific resource to the stream so that the server can properly address the client.
co_await std::visit(
[](auto& client) -> boost::asio::awaitable<void> {
co_await client.CreateResourceBind();
},
client);
co_await std::visit(
[](auto& client) -> boost::asio::awaitable<void> {
co_await client.UpdateListOfContacts();
},
client);
// rfc6120 2.2
// Upon authenticating with a server and binding a resource (thus becoming a connected resource as defined in [XMPPCORE]),
// a client SHOULD request the roster before sending initial presence
co_await std::visit(
[](auto& client) -> boost::asio::awaitable<void> {
SPDLOG_INFO("Send presence: Available");
co_await client.Send(larra::xmpp::presence::c2s::Available{});
},
client);
} catch(const std::exception& err) {
SPDLOG_ERROR("{}", err.what());
co_return;
}
SPDLOG_INFO("Done connecting client!");
}
auto main() -> int {
spdlog::set_level(spdlog::level::trace);
boost::asio::io_context io_context;
boost::asio::co_spawn(io_context, Coroutine(), boost::asio::detached);
io_context.run();
}