当前位置:   article > 正文

基于Qt的MQTT 客户端_qt mqttserver mqttclient

qt mqttserver mqttclient
  1. #ifndef THPNMQTTCLIENT_H
  2. #define THPNMQTTCLIENT_H
  3. #include <QThread>
  4. class QTimer;
  5. class QMqttClient;
  6. class QMqttTopicName;
  7. class thpnMqttClient : public QObject
  8. {
  9. Q_OBJECT
  10. public:
  11. explicit thpnMqttClient(QStringList topics, QObject *parent = nullptr);
  12. ~thpnMqttClient();
  13. void init();
  14. signals:
  15. void receivedMessage(const QByteArray &message);
  16. public slots:
  17. void messageReceived(const QByteArray &message, const QMqttTopicName &topic);
  18. void connected();
  19. void publish(const QByteArray &message,QString subName);
  20. void connectToHost();
  21. private:
  22. QThread m_QThread;
  23. QTimer* timer = nullptr;
  24. QMqttClient *mqtt = nullptr;
  25. QStringList subscribes;
  26. bool isExit = false;
  27. quint16 keepAlive = 3;//second
  28. QString host = "127.0.0.1";
  29. quint16 port = 1883;
  30. };
  31. #endif // THPNMQTTCLIENT_H
  1. #include "thpnmqttclient.h"
  2. #include <QDateTime>
  3. #include <QJsonDocument>
  4. #include <QJsonObject>
  5. #include <QException>
  6. #include <QThread>
  7. #include <QtMqtt>
  8. #include <QTimer>
  9. thpnMqttClient::thpnMqttClient(QStringList topics, QObject *parent) :
  10. QObject(parent),subscribes(topics)
  11. {
  12. connect(&m_QThread, &QThread::started, this, &thpnMqttClient::init);
  13. moveToThread(&m_QThread);
  14. m_QThread.start();
  15. }
  16. thpnMqttClient::~thpnMqttClient()
  17. {
  18. isExit = true;
  19. if(true == m_QThread.isRunning())
  20. {
  21. m_QThread.quit();
  22. m_QThread.wait();
  23. }
  24. try
  25. {
  26. if (nullptr != this->mqtt)
  27. {
  28. for (auto strSubscribe : subscribes)
  29. {
  30. this->mqtt->unsubscribe(strSubscribe);//退订
  31. }
  32. this->mqtt->disconnectFromHost();//断开连接
  33. delete this->mqtt;
  34. this->mqtt = nullptr;
  35. }
  36. if (this->timer != nullptr)
  37. {
  38. this->timer->stop();
  39. delete this->timer;
  40. this->timer = nullptr;
  41. }
  42. }
  43. catch (QException& e)
  44. {
  45. qDebug() << e.what();
  46. }
  47. catch (QString& e)
  48. {
  49. qDebug() << e;
  50. }
  51. catch (const char* e)
  52. {
  53. qDebug() << e;
  54. }
  55. catch (...)
  56. {
  57. qDebug() << "...";
  58. }
  59. }
  60. void thpnMqttClient::init()
  61. {
  62. this->timer = new QTimer();
  63. this->mqtt = new QMqttClient(this);
  64. this->mqtt->setHostname(this->host);
  65. this->mqtt->setPort(this->port);
  66. // mqtt->setCleanSession(true);
  67. connect(this->mqtt, SIGNAL(connected()), this, SLOT(connected()));
  68. connect(this->mqtt, SIGNAL(messageReceived(const QByteArray, const QMqttTopicName)),
  69. this, SLOT(messageReceived(const QByteArray, const QMqttTopicName)));
  70. this->mqtt->setKeepAlive(this->keepAlive);
  71. connect(this->timer, SIGNAL(timeout()), this, SLOT(connectToHost()));
  72. this->timer->start();
  73. }
  74. void thpnMqttClient::connectToHost()
  75. {
  76. if (this->mqtt->state() != QMqttClient::Connected)
  77. {
  78. this->mqtt->connectToHost();
  79. }
  80. }
  81. void thpnMqttClient::messageReceived(const QByteArray& message, const QMqttTopicName& topic)
  82. {
  83. // qInfo()<<"messageReceived:"<<topic.name()<<QString(message);
  84. emit receivedMessage(message);
  85. // publish(message,topic.name());//回执原消息
  86. }
  87. void thpnMqttClient::connected()
  88. {
  89. this->timer->setInterval(this->keepAlive * 1000);
  90. for(auto topic:subscribes)
  91. {
  92. // qDebug()<<"subscribe:"<<topic;
  93. this->mqtt->subscribe(topic);
  94. }
  95. }
  96. void thpnMqttClient::publish(const QByteArray &message,QString subName)
  97. {
  98. if(nullptr == this->mqtt || QMqttClient::Connected != this->mqtt->state())
  99. {
  100. qDebug()<<"The Mqtt Server Not Connected !";
  101. return;
  102. }
  103. if (this->mqtt->publish(subName, message) == -1)
  104. {
  105. qDebug()<<"Could not publish message";
  106. }
  107. // qDebug()<<"publish:"<<subName<<message;
  108. }

编译MQTT库到Qt下

在项目的pro文件中使用

QT += mqtt

如果需要自己封装类,引入头文件

#include <QtMqtt>

MQTT当然是传输json文件比较方便,很方便对接阿里云,百度云,腾讯云,还有各种嵌入式系统,什么都2020年了,你还不知道mqtt协议?!感觉学习吧!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/722349
推荐阅读
相关标签
  

闽ICP备14008679号