新乡移动网站建设,龙湖什么网站做宣传,hm网上商城,中国十大建筑公司文章目录 QMQTT快速入门环境搭建mosquitto 服务器和客户端配置服务器配置客户端配置模拟MQTT的发布订阅 QMQTT - Windows下的客户端项目代码展示遇到的问题 QMQTT快速入门
环境搭建
准备一台linux设备和一台windows设备虚拟机也是可以的#xff1b;安装mosquitto #xff1… 文章目录 QMQTT快速入门环境搭建mosquitto 服务器和客户端配置服务器配置客户端配置模拟MQTT的发布订阅 QMQTT - Windows下的客户端项目代码展示遇到的问题 QMQTT快速入门
环境搭建
准备一台linux设备和一台windows设备虚拟机也是可以的安装mosquitto 准备QMQTT环境 - windows下
mosquitto 服务器和客户端配置
服务器配置
首先安装mosquitto
sudo apt install mosquitto之后我们对配置文件进行修改
sudo vim /etc/mosquitto/mosquitto.conf添加下面的内容
allow_anonymous false
password_file /etc/mosquitto/pwfile.example我们可以新建用户
mosquitto_passwd -c /etc/mosquitto/pwfile.example test之后通过如下命令查询服务占用的端口
lsof -i | grep mosquitto客户端配置
首先安装客户端
sudo apt install mosquitto-clients模拟MQTT的发布订阅
先重新打开一个终端之后订阅一个mqtt的主题
mosquitto_sub -h localhost -p 1883 -u test -P 123456 -t ccc再打开一个客户端发布对应主题的消息
mosquitto_pub -h localhost -p 1883 -u test -P 123456 -t ccc -m Hello World这个时候我们可以看到对应的消息可以正常的收发则测试成功。
QMQTT - Windows下的客户端
编译会有单独的专栏这里不做介绍
我这里使用我自己编译好的QMQTT环境
项目代码展示
暂时还是简单的后续会将功能逐步完善
CMakeList.txt
cmake_minimum_required(VERSION 3.23)
project(MQTTTest)
#指定C标准
set(CMAKE_CXX_STANDARD 17)
#指定输出目录
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${PROJECT_SOURCE_DIR}/output)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${PROJECT_SOURCE_DIR}/output)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${PROJECT_SOURCE_DIR}/output)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${PROJECT_SOURCE_DIR}/output)
#自动编译QT文件
#set(CMAKE_PREFIX_PATH C:/Qt/6.5.1/msvc2019_64)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
#开启包含当前编译目录
set(CMAKE_INCLUDE_CURRENT_DIR ON)
#指定QT版本和对应的库
set(QT_VERSION 5)
set(REQUIRED_LIBS Core Gui Widgets Network# Core5Compat)
set(REQUIRED_LIBS_QUALIFIED Qt${QT_VERSION}::Core Qt${QT_VERSION}::Gui Qt${QT_VERSION}::Widgets Qt${QT_VERSION}::Network# Qt${QT_VERSION}::Core5Compat)
#寻找QT库
find_package(Qt${QT_VERSION} COMPONENTS ${REQUIRED_LIBS} REQUIRED)
include_directories(srcthird_party/qmqtt/include)
file(GLOB HEADERSsrc/*.h)
file(GLOB SOURCESsrc/*.cpp)file(GLOB UISsrc/*.ui)
# 指定格式为utf-8
add_compile_options($$C_COMPILER_ID:MSVC:/utf-8)
add_compile_options($$CXX_COMPILER_ID:MSVC:/utf-8)#增减windows库文件
if(WIN32)set(PLAT_FROM_DEPws2_32.lib)
endif()file(GLOB LIB_MQTT ${PROJECT_SOURCE_DIR}/third_party/qmqtt/lib/*.lib)
link_directories(${PROJECT_SOURCE_DIR}/third_party/qmqtt/lib)#使用指定的源文件来生成目标可执行文件
add_executable(${PROJECT_NAME} main.cpp ${HEADERS} ${SOURCES} ${UIS})
if(WIN32)target_link_libraries(${PROJECT_NAME} ${PLAT_FROM_DEP})
endif()
target_link_libraries(${PROJECT_NAME} ${REQUIRED_LIBS_QUALIFIED} ${LIB_MQTT})main.cpp
#include QApplication
#include mainwindow.h
int main(int argc, char **argv)
{QApplication app(argc, argv);MainWindow mm;mm.show();return app.exec();
}mainwindow.h
#ifndef MQTTTEST_MAINWINDOW_H
#define MQTTTEST_MAINWINDOW_H
#include QMainWindow
#include qmqtt.h
class MainWindow : public QMainWindow
{Q_OBJECT
public:explicit MainWindow(QWidget *parent 0);~MainWindow() override;
protected:void initUi();
protected slots:void connectToHost();void connectSuccess();void error(const QMQTT::ClientError error);
private:QStatusBar *status_bar_;QMQTT::Client *client_;
};
#endif //MQTTTEST_MAINWINDOW_Hmainwindow.cpp
#include mainwindow.h
#include QMenuBar
#include QMenu
#include QStatusBar
#include QAction
#include QHostAddress
#include iostreamMainWindow::MainWindow(QWidget *parent): QMainWindow(parent)
{initUi();client_ new QMQTT::Client(QHostAddress(192.168.0.113), 1883, this);client_-setUsername(test);client_-setPassword(123456);connect(client_, QMQTT::Client::connected, this, MainWindow::connectSuccess);connect(client_, QMQTT::Client::error, this, MainWindow::error);
}MainWindow::~MainWindow()
{}void MainWindow::initUi()
{QMenuBar *menuBar new QMenuBar(this);setMenuBar(menuBar);QMenu *menu new QMenu(操作, this);menuBar-addMenu(menu);QAction *action new QAction(连接服务器, this);menu-addAction(action);status_bar_ new QStatusBar(this);setStatusBar(status_bar_);connect(action, QAction::triggered, this, MainWindow::connectToHost);
}void MainWindow::connectToHost()
{client_-connectToHost();
}void MainWindow::connectSuccess()
{status_bar_-showMessage(连接成功);client_-subscribe(ccc);connect(client_, QMQTT::Client::received, [this](const QMQTT::Message message){std::cout message.topic().toStdString() std::endl;std::cout message.payload().toStdString() std::endl;});
}void MainWindow::error(const QMQTT::ClientError error)
{std::cout error std::endl;
}
遇到的问题
连接失败 - 拒绝连接
需要mosquitto 如下
# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.examplepid_file /run/mosquitto/mosquitto.pidpersistence true
persistence_location /var/lib/mosquitto/log_dest file /var/log/mosquitto/mosquitto.loginclude_dir /etc/mosquitto/conf.dlistener 1883allow_anonymous falsepassword_file /etc/mosquitto/pwfile.example需要添加openssl动态库