链接
最近作业要求,研究了一下MQTT(只能说是简单的实现了一下),顺便使用QT写了一个测试工具
下面是对每一个功能的详解
QMQTT 是QT 提供的MQTT 依赖,需要进行编译后,加载lib文件(有些说的是.a 文件,但我编译完成后就只有lib 文件,尴尴尬尬)。
教程转:链接
private:
Ui::MainWindow *ui;
QMQTT::Client * client;
private slots:
void onMQTT_Received(const QMQTT::Message &message);
void onMQTT_subscribed(const QString& topic);
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);
}
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_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();
}
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
#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
#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();
}