mirror of
https://github.com/boostorg/mqtt5.git
synced 2025-07-29 20:17:37 +02:00
Examples rework
Summary: related to T15261, T15262 - all examples are added to CI checks - as a result, CMakeLists.txt in example folder is reworked to build all examples instead of one - also removed chapter Building with CMake temporarily (it will be back) - all examples accept runtime arguments (brokers, port, clientid) (default: hivemq public broker) - implemented example suggestions by R.Perez, most notable: - all examples include mqtt headers they use (no more unified headers) - multithreaded examples use thread_pool instead of asio::io_context - all examples use logger with log_level::info by default Reviewers: ivica Reviewed By: ivica Subscribers: iljazovic, miljen Differential Revision: https://repo.mireo.local/D32602
This commit is contained in:
@ -7,21 +7,30 @@
|
||||
|
||||
//[hello_world_over_tls
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include <boost/asio/io_context.hpp>
|
||||
#include <boost/asio/detached.hpp>
|
||||
#include <boost/asio/ssl.hpp>
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
|
||||
#include <async_mqtt5.hpp>
|
||||
#include <async_mqtt5/logger.hpp>
|
||||
#include <async_mqtt5/mqtt_client.hpp>
|
||||
#include <async_mqtt5/types.hpp>
|
||||
|
||||
struct config {
|
||||
std::string brokers = "broker.hivemq.com";
|
||||
uint16_t port = 8883; // 8883 is the default TLS MQTT port.
|
||||
std::string client_id = "async_mqtt5_tester";
|
||||
};
|
||||
|
||||
// External customization point.
|
||||
namespace async_mqtt5 {
|
||||
|
||||
// Specify that the TLS handshake will be performed as a client.
|
||||
template <typename StreamBase>
|
||||
struct tls_handshake_type<boost::asio::ssl::stream<StreamBase>> {
|
||||
static constexpr auto client = boost::asio::ssl::stream_base::client;
|
||||
static constexpr auto server = boost::asio::ssl::stream_base::server;
|
||||
};
|
||||
|
||||
// This client uses this function to indicate which hostname it is
|
||||
@ -37,49 +46,49 @@ void assign_tls_sni(
|
||||
|
||||
} // end namespace async_mqtt5
|
||||
|
||||
// The certificate file in the PEM format.
|
||||
constexpr char ca_cert[] =
|
||||
"-----BEGIN CERTIFICATE-----\n"
|
||||
"...........................\n"
|
||||
"-----END CERTIFICATE-----\n"
|
||||
;
|
||||
int main(int argc, char** argv) {
|
||||
config cfg;
|
||||
|
||||
if (argc == 4) {
|
||||
cfg.brokers = argv[1];
|
||||
cfg.port = uint16_t(std::stoi(argv[2]));
|
||||
cfg.client_id = argv[3];
|
||||
}
|
||||
|
||||
int main() {
|
||||
boost::asio::io_context ioc;
|
||||
|
||||
// Context satisfying ``__TlsContext__`` requirements that the underlying SSL stream will use.
|
||||
// TLS context that the underlying SSL stream will use.
|
||||
// The purpose of the context is to allow us to set up TLS/SSL-related options.
|
||||
// See ``__SSL__`` for more information and options.
|
||||
boost::asio::ssl::context context(boost::asio::ssl::context::tls_client);
|
||||
|
||||
async_mqtt5::error_code ec;
|
||||
|
||||
// Add the trusted certificate authority for performing verification.
|
||||
context.add_certificate_authority(boost::asio::buffer(ca_cert), ec);
|
||||
if (ec)
|
||||
std::cout << "Failed to add certificate authority!" << std::endl;
|
||||
ec.clear();
|
||||
|
||||
// Set peer verification mode used by the context.
|
||||
// This will verify that the server's certificate is valid and signed by a trusted certificate authority.
|
||||
context.set_verify_mode(boost::asio::ssl::verify_peer, ec);
|
||||
if (ec)
|
||||
std::cout << "Failed to set peer verification mode!" << std::endl;
|
||||
ec.clear();
|
||||
// Set up the TLS context.
|
||||
// This step is highly dependent on the specific requirements of the Broker you are connecting to.
|
||||
// Each broker may have its own standards and expectations for establishing a secure TLS/SSL connection.
|
||||
// This can include verifying certificates, setting up private keys, PSK authentication, and others.
|
||||
|
||||
// Construct the Client with ``__SSL_STREAM__`` as the underlying stream
|
||||
// with ``__SSL_CONTEXT__`` as the ``__TlsContext__`` type.
|
||||
// with ``__SSL_CONTEXT__`` as the ``__TlsContext__`` type
|
||||
// and logging enabled.
|
||||
async_mqtt5::mqtt_client<
|
||||
boost::asio::ssl::stream<boost::asio::ip::tcp::socket>,
|
||||
boost::asio::ssl::context
|
||||
> client(ioc, std::move(context));
|
||||
boost::asio::ssl::context,
|
||||
async_mqtt5::logger
|
||||
> client(ioc, std::move(context), async_mqtt5::logger(async_mqtt5::log_level::info));
|
||||
|
||||
// 8883 is the default TLS MQTT port.
|
||||
client.brokers("<your-mqtt-broker>", 8883)
|
||||
.async_run(boost::asio::detached);
|
||||
|
||||
// If you want to use the Client without logging, initialise it with the following line instead.
|
||||
//async_mqtt5::mqtt_client<
|
||||
// boost::asio::ssl::stream<boost::asio::ip::tcp::socket>,
|
||||
// boost::asio::ssl::context
|
||||
//> client(ioc, std::move(context));
|
||||
|
||||
client.brokers(cfg.brokers, cfg.port) // Broker that we want to connect to.
|
||||
.credentials(cfg.client_id) // Set the Client Identifier. (optional)
|
||||
.async_run(boost::asio::detached); // Start the Client.
|
||||
|
||||
client.async_publish<async_mqtt5::qos_e::at_most_once>(
|
||||
"<topic>", "Hello world!",
|
||||
"async-mqtt5/test", "Hello world!",
|
||||
async_mqtt5::retain_e::no, async_mqtt5::publish_props{},
|
||||
[&client](async_mqtt5::error_code ec) {
|
||||
std::cout << ec.message() << std::endl;
|
||||
|
Reference in New Issue
Block a user