MQTT客户端程序的编写
一、前言
在前面《MQTT服务器的搭建》一文中,我们介绍了EMQX。打开其帮助文档,我们可以看到:
很开心,作为一个主流的Qt软件开发者,看到了熟悉的基于Qt框架的MQTT客户端。你可以直接下载下来,按照说明文档编译、运行和测试。当然,也可以看下面的介绍,咱们自己写一个简单的MQTT程序,实现发布和订阅消息即可。
二、QtMQTT 项目
1、库文件下载、编译和链接
Qt开发MQTT程序有两种方式,一个是Qt官方提供的基于MQTT的封装,一个是第三方(EMQ)开发的用于Qt调用MQTT的接口。我们只介绍第一种,基于Qt官方提供的封装来使用MQTT。
Qt官方虽然在2017年就已经提供了对MQTT的封装,但是并没有正式加入到Qt的标准库里面,所以需要自己下载源码进行编译。
Qt官方介绍文档地址:https://doc.qt.io/QtMQTT/qtmqtt-index.html
Qt官方在github上提供了源代码,地址:https://github.com/qt/qtmqtt
编译项目,可以得到我们需要的Qt5Mqtt.dll 及Qt5Mqtt.lib 库文件。
在Qt中,我们只需要在.pro文件中添加该库文件的链接路径即可。
其次,请记得将你的QtMqtt项目文件夹拷贝到 你指定的编译器下 include文件夹中(如果已经存在请忽略)。如下图
2、客户端项目程序编写
在自己编写之前,我们先看一下
/******************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtMqtt module.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
******************************************************************************/
#ifndef QTMQTTCLIENT_H
#define QTMQTTCLIENT_H
#include <QtMqtt/qmqttglobal.h>
#include <QtMqtt/qmqttauthenticationproperties.h>
#include <QtMqtt/qmqttconnectionproperties.h>
#include <QtMqtt/qmqttpublishproperties.h>
#include <QtMqtt/qmqttsubscription.h>
#include <QtMqtt/qmqttsubscriptionproperties.h>
#include <QtMqtt/qmqtttopicfilter.h>
#include <QtCore/QIODevice>
#include <QtCore/QObject>
#include <QtCore/QSharedPointer>
#include <QtNetwork/QTcpSocket>
QT_BEGIN_NAMESPACE
class QMqttClientPrivate;
class /*Q_MQTT_EXPORT*/ QMqttClient : public QObject
{
public:
enum TransportType {
IODevice = 0,
AbstractSocket,
SecureSocket
};
enum ClientState {
Disconnected = 0,
Connecting,
Connected
};
enum ClientError {
// Protocol states
NoError = 0,
InvalidProtocolVersion = 1,
IdRejected = 2,
ServerUnavailable = 3,
BadUsernameOrPassword = 4,
NotAuthorized = 5,
// Qt states
TransportInvalid = 256,
ProtocolViolation,
UnknownError,
Mqtt5SpecificError
};
enum ProtocolVersion {
MQTT_3_1 = 3,
MQTT_3_1_1 = 4,
MQTT_5_0 = 5
};
private:
Q_OBJECT
Q_ENUMS(ClientState)
Q_ENUMS(ClientError)
Q_PROPERTY(QString clientId READ clientId WRITE setClientId NOTIFY clientIdChanged)
Q_PROPERTY(QString hostname READ hostname WRITE setHostname NOTIFY hostnameChanged)
Q_PROPERTY(quint16 port READ port WRITE setPort NOTIFY portChanged)
Q_PROPERTY(quint16 keepAlive READ keepAlive WRITE setKeepAlive NOTIFY keepAliveChanged)
Q_PROPERTY(ProtocolVersion protocolVersion READ protocolVersion WRITE setProtocolVersion NOTIFY protocolVersionChanged)
Q_PROPERTY(ClientState state READ state WRITE setState NOTIFY stateChanged)
Q_PROPERTY(ClientError error READ error WRITE setError NOTIFY errorChanged)
Q_PROPERTY(QString username READ username WRITE setUsername NOTIFY usernameChanged)
Q_PROPERTY(QString password READ password WRITE setPassword NOTIFY passwordChanged)
Q_PROPERTY(bool cleanSession READ cleanSession WRITE setCleanSession NOTIFY cleanSessionChanged)
Q_PROPERTY(QString willTopic READ willTopic WRITE setWillTopic NOTIFY willTopicChanged)
Q_PROPERTY(QByteArray willMessage READ willMessage WRITE setWillMessage NOTIFY willMessageChanged)
Q_PROPERTY(quint8 willQoS READ willQoS WRITE setWillQoS NOTIFY willQoSChanged)
Q_PROPERTY(bool willRetain READ willRetain WRITE setWillRetain NOTIFY willRetainChanged)
public:
explicit QMqttClient(QObject *parent = nullptr);
void setTransport(QIODevice *device, TransportType transport);
QIODevice *transport() const;
QMqttSubscription *subscribe(const QMqttTopicFilter &topic, quint8 qos = 0);
QMqttSubscription *subscribe(const QMqttTopicFilter &topic,
const QMqttSubscriptionProperties &properties, quint8 qos = 0);
void unsubscribe(const QMqttTopicFilter &topic);
void unsubscribe(const QMqttTopicFilter &topic, const QMqttUnsubscriptionProperties &properties);
Q_INVOKABLE qint32 publish(const QMqttTopicName &topic, const QByteArray &message = QByteArray(),
quint8 qos = 0, bool retain = false);
Q_INVOKABLE qint32 publish(const QMqttTopicName &topic, const QMqttPublishProperties &properties,
const QByteArray &message = QByteArray(),
quint8 qos = 0,
bool retain = false);
bool requestPing();
QString hostname() const;
quint16 port() const;
QString clientId() const;
quint16 keepAlive() const;
ProtocolVersion protocolVersion() const;
Q_INVOKABLE void connectToHost();
#ifndef QT_NO_SSL
Q_INVOKABLE void connectToHostEncrypted(const QString &sslPeerName = QString());
#endif
Q_INVOKABLE void disconnectFromHost();
ClientState state() const;
ClientError error() const;
QString username() const;
QString password() const;
bool cleanSession() const;
QString willTopic() const;
quint8 willQoS() const;
QByteArray willMessage() const;
bool willRetain() const;
void setConnectionProperties(const QMqttConnectionProperties &prop);
QMqttConnectionProperties connectionProperties() const;
void setLastWillProperties(const QMqttLastWillProperties &prop);
QMqttLastWillProperties lastWillProperties() const;
QMqttServerConnectionProperties serverConnectionProperties() const;
void authenticate(const QMqttAuthenticationProperties &prop);
Q_SIGNALS:
void connected();
void disconnected();
void messageReceived(const QByteArray &message, const QMqttTopicName &topic = QMqttTopicName());
void messageStatusChanged(qint32 id, QMqtt::MessageStatus s, const QMqttMessageStatusProperties &properties);
void messageSent(qint32 id);
void pingResponseReceived();
void brokerSessionRestored();
void hostnameChanged(QString hostname);
void portChanged(quint16 port);
void clientIdChanged(QString clientId);
void keepAliveChanged(quint16 keepAlive);
void protocolVersionChanged(ProtocolVersion protocolVersion);
void stateChanged(ClientState state);
void errorChanged(ClientError error);
void usernameChanged(QString username);
void passwordChanged(QString password);
void cleanSessionChanged(bool cleanSession);
void willTopicChanged(QString willTopic);
void willQoSChanged(quint8 willQoS);
void willMessageChanged(QByteArray willMessage);
void willRetainChanged(bool willRetain);
void authenticationRequested(const QMqttAuthenticationProperties &p);
void authenticationFinished(const QMqttAuthenticationProperties &p);
public Q_SLOTS:
void setHostname(const QString &hostname);
void setPort(quint16 port);
void setClientId(const QString &clientId);
void setKeepAlive(quint16 keepAlive);
void setProtocolVersion(ProtocolVersion protocolVersion);
void setState(ClientState state);
void setError(ClientError error);
void setUsername(const QString &username);
void setPassword(const QString &password);
void setCleanSession(bool cleanSession);
void setWillTopic(const QString &willTopic);
void setWillQoS(quint8 willQoS);
void setWillMessage(const QByteArray &willMessage);
void setWillRetain(bool willRetain);
private:
void connectToHost(bool encrypted, const QString &sslPeerName);
Q_DISABLE_COPY(QMqttClient)
Q_DECLARE_PRIVATE(QMqttClient)
};
Q_DECLARE_TYPEINFO(QMqttClient::TransportType, Q_PRIMITIVE_TYPE);
Q_DECLARE_TYPEINFO(QMqttClient::ClientState, Q_PRIMITIVE_TYPE);
Q_DECLARE_TYPEINFO(QMqttClient::ClientError, Q_PRIMITIVE_TYPE);
QT_END_NAMESPACE
Q_DECLARE_METATYPE(QMqttClient::TransportType)
Q_DECLARE_METATYPE(QMqttClient::ClientState)
Q_DECLARE_METATYPE(QMqttClient::ClientError)
#endif // QTMQTTCLIENT_H
重点关注QMqttClient中的信号函数和对应的槽函数,这将是我们使用QMqttClient实例化一个mqtt客户端对象后操作的重点。
SLOTS:
void setHostname(const QString &hostname); //用于设置即将连接的Mqtt服务器。
void setPort(quint16 port); // 设置端口号(与Mqtt服务器一致)
void setUsername(const QString &username); // 设置用户名
void setPassword(const QString &password); // 设置密码void connectToHost(bool encrypted, const QString &sslPeerName); // 连接到服务器,如果需要SSL加密,可以对应设置
SIGNALS:
void connected(); // 当客户端与服务器连接上,此信号将被触发
void disconnected(); // 当客户端与服务器断开连接,此信号将被触发
void messageReceived(const QByteArray &message, const QMqttTopicName &topic = QMqttTopicName()); // 当有消息被接受到,此信号被触发。参数包含消息内容和对应的topic。显然,我们订阅一个主题后,异步接收消息就全靠这个信号了。
void pingResponseReceived(); // MQTT客户端与服务器之间会定时发送ping包,类似于心跳检测客户端与服务器之间的通信是否连接正常。当服务器端有ping包的确认消息返回,此消息被触发。// 下面还有其他的一些状态改变触发的信号,不做详细解释
void hostnameChanged(QString hostname);
void portChanged(quint16 port);
void keepAliveChanged(quint16 keepAlive);
void protocolVersionChanged(ProtocolVersion protocolVersion);
void stateChanged(ClientState state);
void errorChanged(ClientError error);
void usernameChanged(QString username);
void passwordChanged(QString password);
void cleanSessionChanged(bool cleanSession);
记得将mqtt文件夹拷贝到项目文件目录下,如下图:
好了,代码开干:
// mqttclientwindow.h
#ifndef MQTTCLIENTWINDOW_H
#define MQTTCLIENTWINDOW_H
#include <QWidget>
#include "mqtt/qmqttclient.h"
namespace Ui {
class MqttClientWindow;
}
class MqttClientWindow : public QWidget
{
Q_OBJECT
public:
explicit MqttClientWindow(QWidget *parent = nullptr);
~MqttClientWindow();
public slots:
void on_pub_pushButton_clicked();
void on_connect_pushButton_clicked();
void on_sub_pushButton_clicked();
void onMessageReceived(const QByteArray &message, const QMqttTopicName &topic = QMqttTopicName());
void onPingResponseReceived();
void onConnected();
void onDisconnected();
private:
Ui::MqttClientWindow *ui;
QMqttClient* client;
};
#endif // MQTTCLIENTWINDOW_H
// mqttclientwindow.cpp
#include "mqttclientwindow.h"
#include "ui_mqttclientwindow.h"
#include <QDebug>
#include <QDateTime>
#include <QHostAddress>
MqttClientWindow::MqttClientWindow(QWidget *parent) :
QWidget(parent),
ui(new Ui::MqttClientWindow)
{
ui->setupUi(this);
client = new QMqttClient;
QObject::connect(client,SIGNAL(connected()),this,SLOT(onConnected()));
QObject::connect(client,SIGNAL(disconnected()),this,SLOT(onDisconnected()));
QObject::connect(client, SIGNAL(messageReceived(const QByteArray&, const QMqttTopicName&)),this, SLOT(onMessageReceived(const QByteArray &, const QMqttTopicName &)));
QObject::connect(client,SIGNAL(pingResponseReceived()), this, SLOT(onPingResponseReceived()));
}
MqttClientWindow::~MqttClientWindow()
{
if(client->state() == QMqttClient::Connected){
client->disconnectFromHost();
}
delete ui;
}
void MqttClientWindow::on_pub_pushButton_clicked()
{
if(client->publish(ui->pub_topic_lineEdit->text(),ui->pub_message_lineEdit->text().toUtf8()) == -1){
qDebug() <<" Could not publish message";
}
}
void MqttClientWindow::on_connect_pushButton_clicked()
{
//未连接服务器则连接
if (client->state() == QMqttClient::Disconnected) {
ui-> connect_pushButton->setText(tr("Disconnect"));
client->setHostname("127.0.0.1");
client->setPort(ui->port_lineEdit->text().trimmed().toUInt());
client->setUsername("James");
client->setPassword("james");
ui->host_lineEdit->setEnabled(false);
ui->port_lineEdit->setEnabled(false);
client->connectToHost();
} else {//断开连接
ui->connect_pushButton->setText(tr("Connect"));
ui->host_lineEdit->setEnabled(true);
ui->port_lineEdit->setEnabled(true);
client->disconnectFromHost();
}
}
void MqttClientWindow::on_sub_pushButton_clicked()
{
client->subscribe(ui->sub_lineEdit->text());
}
void MqttClientWindow::onMessageReceived(const QByteArray &message, const QMqttTopicName &topic)
{
const QString content = QDateTime::currentDateTime().toString()
+ QLatin1String(" Received Topic: ")
+ topic.name()
+ QLatin1String(" Message: ")
+ message
+ QLatin1Char('\n');
ui->sub_textEdit->append(content);
}
void MqttClientWindow::onPingResponseReceived()
{
const QString content = QDateTime::currentDateTime().toString()
+ QLatin1String(" PingResponse")
+ QLatin1Char('\n');
ui->sub_textEdit->append(content);
}
void MqttClientWindow::onConnected()
{
ui->sub_textEdit->append("has connected to server");
}
void MqttClientWindow::onDisconnected()
{
ui->sub_textEdit->append("has disConnected to server");
}
// main.cpp
#include "mqttclientwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MqttClientWindow w;
w.show();
return a.exec();
}
上一篇: python实现图片批量压缩
下一篇: 做电商,我们不能对价值避而不谈
推荐阅读
-
php编写简单的文章发布程序
-
使用 acl 库编写高效的 C++ redis 客户端应用
-
基于open62541在QT编写OPCUA特定的客户端程序(含有源码) + VS2015 C语言搭建OPCUA客户端环境
-
php编写简单的文章发布程序_PHP
-
C#编写网游客户端的实现
-
编写一个程序,输入一个整数以及要转换的进制,输出转换结果,结果以字符串方式输出,例如 输入10814 16,把10814转换为16进制的字符串“2A3E”,如果输入10814 2,则10814
-
Python编写的客户端给服务器发送指令执行相应的命令并返回结果
-
在eclipse使用map reduce编写word count程序生成jar包并在虚拟机运行的步骤
-
简单的用PHP编写的导航条程序
-
opencv 结合pyqt5 编写简单的图像处理GUI程序