Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QMQTT Client stuck connecting.
Forum Updated to NodeBB v4.3 + New Features

QMQTT Client stuck connecting.

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 732 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RFinchMinicam
    wrote on last edited by
    #1

    I'm trying to connect to a mosquito server running on my machine (default config, anonymous connections allowed). I can see the client connect but its state never progresses to connected and simply times out. Full code below.

    #include <QCoreApplication>
    #include <QThread>
    #include <QtMqtt>
    
    
    int main(int argc, char *argv[]) {
        QCoreApplication a(argc, argv);
    
        const QString hostname{"127.0.0.1"};
        const quint16 port = 1883;
        const QMqttTopicName topic{"Test"};
        const QMqttTopicFilter filter{"Test"};
    
    
        QMqttClient client;
        client.setHostname(hostname);
        client.setPort(port);
    
    
        QObject::connect(&client, &QMqttClient::stateChanged, [](QMqttClient::ClientState state){
            if(state == QMqttClient::Disconnected)
                qDebug() << " State: Disconnected";
            else if(state == QMqttClient::Connecting)
                qDebug() << " State: Connecting";
            else if(state == QMqttClient::Connected)
                qDebug() << " State: Connected";
        });
    
        QObject::connect(&client, &QMqttClient::errorChanged, [](QMqttClient::ClientError error){
            qDebug() << error;
    
        });
    
        QObject::connect(&client, &QMqttClient::messageReceived, [](const QByteArray &message, const QMqttTopicName &topic){
            qDebug() << " Received Topic:" << topic.name() << " Message: " << message;
        });
    
        QTimer timer;
        QObject::connect(&timer, &QTimer::timeout, [&client, &topic](){
            if(client.publish(topic,  QDateTime::currentDateTime().toString().toUtf8()) == -1)
                qDebug() << "Error: Could not publish message";
        });
    
        QObject::connect(&client, &QMqttClient::connected, [&client, &timer, &filter](){
            QMqttSubscription *subscription = client.subscribe(filter);
            if(!subscription)
                qDebug() << "Could not subscribe";
            timer.start(1000);
        });
    
        client.connectToHost();
    
        while(1)
        {
            QThread::sleep(3);
            if(client.state() == QMqttClient::Connecting)
            {
                qDebug() << "Connecting";
            }
            if(client.state() == QMqttClient::Connected)
            {
                qDebug() << "Connected";
            }
            if(client.state() == QMqttClient::Disconnected)
            {
                qDebug() << "Disconnect";
            }
        }
    
        return a.exec();
    }
    
    

    2030da13-e04e-4e86-8973-b48bfdcc4cf7-image.png

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      @RFinchMinicam said in QMQTT Client stuck connecting.:

      while(1)

      You are blocking your application with that endless loop.

      Qt is an asynchronous framework but you do not allow it to start the event loop. Therefore it cannot work properly. Drop that loop.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • R Offline
        R Offline
        RFinchMinicam
        wrote on last edited by
        #3

        That works. So is QThread::sleep blocking? I've not used QT before and typically putting a thread to sleep will unblock for other events.

        Pablo J. RoginaP 1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Your infinite loop never ends, so a.exec(); will never be called therefore there's no events to start with.

          As for QThread::msleep, it makes the current thread sleep that's all. If it happens in the main thread the event loop will be blocked.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • R RFinchMinicam

            That works. So is QThread::sleep blocking? I've not used QT before and typically putting a thread to sleep will unblock for other events.

            Pablo J. RoginaP Offline
            Pablo J. RoginaP Offline
            Pablo J. Rogina
            wrote on last edited by
            #5

            @RFinchMinicam said in QMQTT Client stuck connecting.:

            I've not used QT before

            You may want to take a look and try this client example.
            You'll see there are no infinite loops but the usage of Qt's signals & slots instead.

            Upvote the answer(s) that helped you solve the issue
            Use "Topic Tools" button to mark your post as Solved
            Add screenshots via postimage.org
            Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved