2024-05-27 10:35:06 +02:00
|
|
|
//
|
|
|
|
// Copyright (c) 2023-2024 Ivica Siladic, Bruno Iljazovic, Korina Simicevic
|
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0.
|
|
|
|
// (See accompanying file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
|
2024-02-27 09:00:54 +01:00
|
|
|
//[hello_world_over_tcp
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include <boost/asio/io_context.hpp>
|
|
|
|
#include <boost/asio/detached.hpp>
|
|
|
|
#include <boost/asio/ip/tcp.hpp>
|
|
|
|
|
|
|
|
#include <async_mqtt5.hpp>
|
|
|
|
|
|
|
|
int main() {
|
2024-11-29 09:53:34 +01:00
|
|
|
//[init_tcp_client
|
|
|
|
// Initialize the execution context required to run I/O operations.
|
2024-02-27 09:00:54 +01:00
|
|
|
boost::asio::io_context ioc;
|
|
|
|
|
|
|
|
// Construct the Client with ``__TCP_SOCKET__`` as the underlying stream.
|
2024-12-02 15:14:47 +01:00
|
|
|
async_mqtt5::mqtt_client<boost::asio::ip::tcp::socket> client(ioc);
|
2024-11-29 09:53:34 +01:00
|
|
|
//]
|
2024-02-27 09:00:54 +01:00
|
|
|
|
2024-11-29 09:53:34 +01:00
|
|
|
//[configure_tcp_client
|
2024-02-27 09:00:54 +01:00
|
|
|
// 1883 is the default TCP MQTT port.
|
2024-05-03 11:27:11 +02:00
|
|
|
client.brokers("broker.hivemq.com", 1883)
|
2024-11-29 09:53:34 +01:00
|
|
|
.credentials("async_mqtt5_tester")
|
2024-02-27 09:00:54 +01:00
|
|
|
.async_run(boost::asio::detached);
|
2024-11-29 09:53:34 +01:00
|
|
|
//]
|
2024-02-27 09:00:54 +01:00
|
|
|
|
2024-11-29 09:53:34 +01:00
|
|
|
//[publish_hello_world
|
2024-02-27 09:00:54 +01:00
|
|
|
client.async_publish<async_mqtt5::qos_e::at_most_once>(
|
2024-05-03 11:27:11 +02:00
|
|
|
"async-mqtt5/test", "Hello world!",
|
|
|
|
async_mqtt5::retain_e::yes, async_mqtt5::publish_props {},
|
2024-02-27 09:00:54 +01:00
|
|
|
[&client](async_mqtt5::error_code ec) {
|
|
|
|
std::cout << ec.message() << std::endl;
|
2024-11-29 09:53:34 +01:00
|
|
|
|
|
|
|
// Disconnnect the Client.
|
2024-02-27 09:00:54 +01:00
|
|
|
client.async_disconnect(boost::asio::detached);
|
|
|
|
}
|
|
|
|
);
|
2024-11-29 09:53:34 +01:00
|
|
|
//]
|
2024-02-27 09:00:54 +01:00
|
|
|
|
|
|
|
ioc.run();
|
|
|
|
}
|
|
|
|
//]
|