Selaa lähdekoodia

fix reconnect mqtt server

Yao 1 kuukausi sitten
vanhempi
commit
9fdf19b5b1
1 muutettua tiedostoa jossa 24 lisäystä ja 19 poistoa
  1. 24 19
      mqtt/mqttclient.cpp

+ 24 - 19
mqtt/mqttclient.cpp

@@ -11,9 +11,31 @@
 MqttClient::MqttClient(QObject *parent) : QObject(parent), _mqClient(new QMqttClient(this)) {
     // 连接信号和槽
     connect(_mqClient, &QMqttClient::stateChanged, this, &MqttClient::onStateChanged);
+    connect(_mqClient, &QMqttClient::disconnected, this, [this]() {
+        QMetaObject::invokeMethod(
+            this,
+            [this]() {
+                QMessageBox msgBox;
+                msgBox.setWindowTitle("MQtt 错误");
+                msgBox.setText("mqtt连接失败, 请检查网络连接或MQTT服务器状态");
+                QPushButton *yesButton = msgBox.addButton("重连", QMessageBox::YesRole);
+                QPushButton *noButton = msgBox.addButton("取消", QMessageBox::NoRole);
+                yesButton->setStyleSheet(
+                    "QPushButton { background-color:rgb(5, 58, 156); color: white; padding: 5px 15px; "
+                    "border-radius: 4px; }");
+                noButton->setStyleSheet(
+                    "QPushButton { background-color: #f44336; color: white; padding: 5px 15px; "
+                    "border-radius: 4px; }");
+                msgBox.exec();
+                if (msgBox.clickedButton() == yesButton) {
+                    _mqClient->connectToHost();
+                }
+            },
+            Qt::ConnectionType::QueuedConnection);
+    });
     connect(_mqClient, &QMqttClient::messageReceived, this, &MqttClient::onMessageReceived);
     connect(_mqClient, &QMqttClient::errorChanged, this, &MqttClient::onError);
-    connect(_mqClient, &QMqttClient::connected, this, &MqttClient::onConnected);
+    // connect(_mqClient, &QMqttClient::connected, this, &MqttClient::onConnected);
 }
 
 void MqttClient::connectToMqttBroker(const QString &hostname, quint16 port, const QString &username,
@@ -34,7 +56,7 @@ void MqttClient::connectToMqttBroker(const QString &hostname, quint16 port, cons
 }
 void MqttClient::onConnected() {
     Logger::getInstance().debug("MQTT conncted");
-    subscribeToTopics(m_subscribeTopics);
+    // subscribeToTopics(m_subscribeTopics);
 }
 
 void MqttClient::subscribeToTopics(const QStringList &topics) {
@@ -83,23 +105,6 @@ void MqttClient::onMessageReceived(const QByteArray &message, const QMqttTopicNa
 void MqttClient::onStateChanged(QMqttClient::ClientState state) {
     switch (state) {
         case QMqttClient::ClientState::Disconnected: {
-            QMessageBox msgBox;
-            msgBox.setWindowTitle("MQtt 错误");
-            msgBox.setText("mqtt连接失败, 请检查网络连接或MQTT服务器状态");
-            QPushButton *yesButton = msgBox.addButton("重连", QMessageBox::YesRole);
-            QPushButton *noButton = msgBox.addButton("取消", QMessageBox::NoRole);
-            yesButton->setStyleSheet(
-                "QPushButton { background-color:rgb(5, 58, 156); color: white; padding: 5px 15px; "
-                "border-radius: 4px; }");
-            noButton->setStyleSheet(
-                "QPushButton { background-color: #f44336; color: white; padding: 5px 15px; "
-                "border-radius: 4px; }");
-            msgBox.exec();
-            if (msgBox.clickedButton() == yesButton) {
-                // TODO
-                _mqClient->connectToHost();
-                break;
-            }
             Logger::getInstance().info("mqtt disconnected");
             break;
         }