0
点赞
收藏
分享

微信扫一扫

Docker:基础概念、架构与网络模式详解

玩物励志老乐 2024-07-24 阅读 28

文章目录

😏1. 项目介绍

项目Github地址:https://github.com/eclipse/paho.mqtt.cpp

paho.mqtt.cpp是一个用于C++的MQTT客户端库,由Eclipse Paho项目提供支持。它提供了MQTT协议的实现,允许开发者在其C++应用程序中轻松地集成MQTT通信功能。

主要特点和用途:

😊2. 环境配置

ubuntu源码安装paho.mqtt.cpp:

# 依赖paho.mqtt.c
git clone https://github.com/eclipse/paho.mqtt.c.git
mkdir build && cd build
cmake .. -DPAHO_WITH_SSL=ON
make
sudo make install
sudo ldconfig
# 安装paho.mqtt.cpp
git clone https://github.com/eclipse/paho.mqtt.cpp
mkdir build && cd build
cmake ..
make
sudo make install
sudo ldconfig

程序g++编译:

g++ -o main main.cpp -lpaho-mqttpp3 -lpaho-mqtt3cs

😆3. 使用说明

mqtt发布和订阅示例:

#include <iostream>
#include <mqtt/async_client.h>

const std::string SERVER_ADDRESS("tcp://localhost:1883");
const std::string CLIENT_ID("paho_cpp_async");
const std::string TOPIC("test/topic");

int main(int argc, char* argv[]) {
    mqtt::async_client client(SERVER_ADDRESS, CLIENT_ID);

    mqtt::connect_options connOpts;
    connOpts.set_keep_alive_interval(20);
    connOpts.set_clean_session(true);

    try {
        client.connect(connOpts)->wait();

        client.subscribe(TOPIC, 1)->wait();

        mqtt::message_ptr msg = mqtt::make_message(TOPIC, "Hello from paho mqtt cpp!");
        client.publish(msg)->wait();

        client.disconnect()->wait();
    }
    catch (const mqtt::exception& exc) {
        std::cerr << "Error: " << exc.what() << std::endl;
        return 1;
    }

    return 0;
}

在这里插入图片描述

以上。

举报

相关推荐

0 条评论