QT 实现MQTT 协议通信

MQTT 接收

链接

最近作业要求,研究了一下MQTT(只能说是简单的实现了一下),顺便使用QT写了一个测试工具
下面是对每一个功能的详解

1、EMQ QMQTT

QMQTT 是QT 提供的MQTT 依赖,需要进行编译后,加载lib文件(有些说的是.a 文件,但我编译完成后就只有lib 文件,尴尴尬尬)。
教程转:链接

2、界面设计

QT 实现MQTT 协议通信_第1张图片

3、代码

3.1 初始化

  1. 创建对象,声明方法
private:
    Ui::MainWindow *ui;
    QMQTT::Client * client;
private slots:
    void onMQTT_Received(const QMQTT::Message &message);
    void onMQTT_subscribed(const QString& topic);
  1. 构造器初始化
client = new Client();

    connect(client,SIGNAL(received(const QMQTT::Message&)),this,SLOT(onMQTT_Received(const QMQTT::Message&)));
    connect(client,SIGNAL(subscribed(const QString&)),this,SLOT(onMQTT_subscribed(const QString&)));

对应方法实现

void MainWindow::onMQTT_Received(const QMQTT::Message &message)
{
	// 接受到服务器消息时的处理
    QString strPaylaod = message.payload();
    //QMessageBox::warning(this,"payload",strPaylaod,QMessageBox::Yes,QMessageBox::No);
    QString strLine = ui->textBrowser_msg->toPlainText();

    QDateTime dt = QDateTime::currentDateTime();
    QString strDate = dt.toString(Qt::SystemLocaleLongDate);//日期格式自定义
    strLine += "\n";
    strLine += strDate;
    strLine += "\n";
    strLine += strPaylaod;

    ui->textBrowser_msg->setText(strLine);

}
void MainWindow::onMQTT_subscribed(const QString &topic)
{

	// 订阅成功后的处理
    qDebug() << topic;
     QMessageBox::warning(this,"订阅成功",topic,QMessageBox::Yes);

}

3.2 连接服务器

void MainWindow::on_pushButton_conn_clicked()
{
    // 取出ip,端口等数据
    QHostAddress host(QString(ui->lineEdit_ip->text()));
    quint16 port = ui->spinBox->value();
    quint16 QosNum = ui->spinBox_QoS->value();
    QString cId = ui->lineEdit_clientId->text();
    QString username = ui->lineEdit_username->text();
    QString psword = ui->lineEdit_psword->text();
    QString ip = ui->lineEdit_ip->text();

    if(cId == "" || username == "" || psword == "" || ip == "")
    {
        QMessageBox::warning(this,"提示","请将信息输入完整",QMessageBox::Yes);
        return;
    }

	// 对客户端对象设置属性
    client->setHost(host);
    client->setPort(port);
    client->setWillQos(QosNum);
    client->setClientId(cId);

	// 建立连接
    client->connectToHost();


}

3.3 发送消息(这里有一些问题,不知道是我那写错了,希望大佬帮忙改改)

问题:每次发送消息后,断开连接才另一端才能接受到,尴尴尬尬

void MainWindow::on_pushButton_send_clicked()
{
    // 发送消息
   /* client->setWillMessage("111111");
    client->disconnectFromHost();
    client->connectToHost();*/


    // 测试代码
    client->disconnectFromHost();
    QHostAddress host(QString(ui->lineEdit_ip->text()));
    quint16 port = ui->spinBox->value();
    quint16 QosNum = ui->spinBox_QoS->value();
    QString cId = ui->lineEdit_clientId->text();
    QString username = ui->lineEdit_username->text();
    QString psword = ui->lineEdit_psword->text();
    QString ip = ui->lineEdit_ip->text();

    if(cId == "" || username == "" || psword == "" || ip == "")
    {
        QMessageBox::warning(this,"提示","请将信息输入完整",QMessageBox::Yes);
        return;
    }

    client->setHost(host);
    client->setPort(port);
    client->setWillQos(QosNum);
    client->setClientId(cId);
    client->connectToHost();
    QByteArray qbyte = ui->lineEdit_sendMsg->text().toUtf8();
    client->setWillMessage(qbyte);
    client->disconnectFromHost();

    // 恢复连接
    client->connectToHost();

}

完整代码

UI


 MainWindow
 
  
   
    0
    0
    380
    501
   
  
  
   MainWindow
  
  
   
    
     
      60
      150
      133
      26
     
    
   
   
    
     
      10
      150
      48
      26
     
    
    
     CID:
    
   
   
    
     
      10
      190
      261
      31
     
    
   
   
    
     
      280
      190
      81
      31
     
    
    
     发送
    
   
   
    
     
      60
      20
      133
      121
     
    
    
     Qt::Vertical
    
    
     
      Qt::Vertical
     
     
      
       Qt::Vertical
      
      
       
        QLineEdit::Normal
       
      
      
       
        QLineEdit::Password
       
      
      
      
     
    
   
   
    
     
      10
      20
      48
      121
     
    
    
     Qt::Vertical
    
    
     
      用户名:
     
    
    
     
      密码:
     
    
    
     
      测试IP:
     
    
    
     
      订阅:
     
    
   
   
    
     
      20
      250
      351
      204
     
    
    
     Qt::Vertical
    
    
     
      信息
     
    
    
   
   
    
     
      200
      20
      161
      121
     
    
    
     
      271
      16777215
     
    
    
     Qt::Vertical
    
    
     
      
       271
       16777215
      
     
     
      Qt::Vertical
     
     
      
       
        271
        16777215
       
      
      
       Qt::Vertical
      
      
       
        
         271
         16777215
        
       
       
        连接
       
      
      
       
        
         271
         16777215
        
       
       
        断开
       
      
     
     
      
       
        271
        16777215
       
      
      
       Qt::Horizontal
      
      
       
        
         150
         16777215
        
       
       
        端口:
       
      
      
       
        
         35
         20
        
       
       
        
         116
         16777215
        
       
      
     
    
    
     
      
       271
       16777215
      
     
     
      订阅
     
    
   
   
    
     
      200
      150
      161
      31
     
    
    
     
      
       
        
         271
         16777215
        
       
       
        生成CID
       
      
     
     
      
       
        
         150
         16777215
        
       
       
        QoS:
       
      
     
     
      
       
        
         35
         20
        
       
       
        
         116
         16777215
        
       
      
     
    
   
  
  
   
    
     0
     0
     380
     23
    
   
  
  
 
 
 
 


MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
#include 

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();


private slots:
    void onMQTT_Received(const QMQTT::Message &message);
    void onMQTT_subscribed(const QString& topic);

    void on_pushButton_conn_clicked();

    void on_pushButton_disconn_clicked();

    void on_pushButton_dy_clicked();

    void on_pushButton_clientId_clicked();

    void on_pushButton_send_clicked();

private:
    Ui::MainWindow *ui;
    QMQTT::Client * client;

};
#endif // MAINWINDOW_H

MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include 
#include 
#include 
#include 
#include 
using namespace QMQTT;

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{

    ui->setupUi(this);
    ui->spinBox->setMaximum(99999);
    ui->spinBox->setValue(1883);
    ui->spinBox_QoS->setMaximum(2);
    ui->spinBox_QoS->setMinimum(0);

    client = new Client();

    connect(client,SIGNAL(received(const QMQTT::Message&)),this,SLOT(onMQTT_Received(const QMQTT::Message&)));
    connect(client,SIGNAL(subscribed(const QString&)),this,SLOT(onMQTT_subscribed(const QString&)));

}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::onMQTT_Received(const QMQTT::Message &message)
{
    QString strPaylaod = message.payload();
    //QMessageBox::warning(this,"payload",strPaylaod,QMessageBox::Yes,QMessageBox::No);
    QString strLine = ui->textBrowser_msg->toPlainText();

    QDateTime dt = QDateTime::currentDateTime();
    QString strDate = dt.toString(Qt::SystemLocaleLongDate);//日期格式自定义
    strLine += "\n";
    strLine += strDate;
    strLine += "\n";
    strLine += strPaylaod;

    ui->textBrowser_msg->setText(strLine);

}
void MainWindow::onMQTT_subscribed(const QString &topic)
{

    qDebug() << topic;
     QMessageBox::warning(this,"订阅成功",topic,QMessageBox::Yes);

}

void MainWindow::on_pushButton_conn_clicked()
{
    // 取出ip,端口
    QHostAddress host(QString(ui->lineEdit_ip->text()));
    quint16 port = ui->spinBox->value();
    quint16 QosNum = ui->spinBox_QoS->value();
    QString cId = ui->lineEdit_clientId->text();
    QString username = ui->lineEdit_username->text();
    QString psword = ui->lineEdit_psword->text();
    QString ip = ui->lineEdit_ip->text();

    if(cId == "" || username == "" || psword == "" || ip == "")
    {
        QMessageBox::warning(this,"提示","请将信息输入完整",QMessageBox::Yes);
        return;
    }

    client->setHost(host);
    client->setPort(port);
    client->setWillQos(QosNum);
    client->setClientId(cId);
    client->connectToHost();


}

void MainWindow::on_pushButton_disconn_clicked()
{
    client->disconnectFromHost();
}

void MainWindow::on_pushButton_dy_clicked()
{
    client->setWillTopic(ui->lineEdit_dy->text());
    client->subscribe(ui->lineEdit_dy->text(), 0);
}

void MainWindow::on_pushButton_clientId_clicked()
{
    qsrand(QDateTime::currentMSecsSinceEpoch());//为随机值设定一个seed

    const char chrs[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    int chrs_size = sizeof(chrs);

    char* ch = new char[chrs_size + 1];
    memset(ch, 0, chrs_size + 1);
    int randomx = 0;
    for (int i = 0; i < 20; ++i)
    {
        randomx= rand() % (chrs_size - 1);
        ch[i] = chrs[randomx];
    }

    QString ret(ch);
    delete[] ch;
    ui->lineEdit_clientId->setText(ret);

}

void MainWindow::on_pushButton_send_clicked()
{
    // 发送消息
   /* client->setWillMessage("111111");
    client->disconnectFromHost();
    client->connectToHost();*/


    // 测试代码
    client->disconnectFromHost();
    QHostAddress host(QString(ui->lineEdit_ip->text()));
    quint16 port = ui->spinBox->value();
    quint16 QosNum = ui->spinBox_QoS->value();
    QString cId = ui->lineEdit_clientId->text();
    QString username = ui->lineEdit_username->text();
    QString psword = ui->lineEdit_psword->text();
    QString ip = ui->lineEdit_ip->text();

    if(cId == "" || username == "" || psword == "" || ip == "")
    {
        QMessageBox::warning(this,"提示","请将信息输入完整",QMessageBox::Yes);
        return;
    }

    client->setHost(host);
    client->setPort(port);
    client->setWillQos(QosNum);
    client->setClientId(cId);
    client->connectToHost();
    QByteArray qbyte = ui->lineEdit_sendMsg->text().toUtf8();
    client->setWillMessage(qbyte);
    client->disconnectFromHost();

    // 恢复连接
    client->connectToHost();

}

你可能感兴趣的:(C/C++)