qmqtt auto reconnect on disconnet
-
Hi
I am connecting to the qmqtt
m_client = new QMqttClient(this); m_client->setHostname("localhost"); m_client->setPort(1883); m_client->setClientId("RASPBERRY-009"); m_client->setUsername("pullu"); m_client->setPassword("pullu"); m_client->setKeepAlive(30); m_client->setCleanSession(false); m_client->connectToHost(); QObject::connect(m_client, &QMqttClient::stateChanged, this, &Publisher::updateStateChange); QObject::connect(m_client, &QMqttClient::connected, this, &Publisher::sendMessages);
When its connected looking a folder new files than sending to the emqt broker.
There sometimes network problem or broker restart etc. Than client lost his connection and state change 1.
than :
void Publisher::updateStateChange() { const QString content = QDateTime::currentDateTime().toString() + QLatin1String(": State Changed : ") + QString::number(m_client->state()); qDebug() << " -- *** ** state change .. : " << content << endl; while (m_client->state() != 2 ){ qDebug() << " Retrying.... . . . " << endl; m_client->setHostname("localhost"); m_client->setPort(1883); m_client->setClientId("RASPBERRY-009"); m_client->setUsername("pullu"); m_client->setPassword("pullu"); m_client->setKeepAlive(30); m_client->setCleanSession(false); m_client->connectToHost(); const QString content = QDateTime::currentDateTime().toString() + QLatin1String(": State Changed : ") + QString::number(m_client->state()); qDebug() << " -- *** ** state === .. : " << content << endl; QThread::sleep(2); }
I am checking state than try to recover my connection .
I couldt succed.
How can I keep my client always connected if bad things happen try to reconnect untill connected :)
Best
-
@RahibeMeryem said in qmqtt auto reconnect on disconnet:
while (m_client->state() != 2 ){
NEVER use such loops and QThread::sleep(2); in event driven applications!
You're blocking Qt event loop!Use a QTimer for reconnect.
-
I figured it out before read this :)
Another question for same topic:
can I connect signals from threads signal from it ? like :
Messenqt::Messenqt(QObject *parent) : QObject(parent) { m_client = new QMqttClient(this); m_client->setHostname("localhost"); m_client->setPort(1883); m_client->setClientId("ERRY-009"); m_client->setUsername("xxxxx"); m_client->setPassword("xxxxx"); m_client->setKeepAlive(300); m_client->setCleanSession(false); m_client->connectToHost(); //yeni slot mekanizmasi. kendi icinde &Publisher örneği.. QObject::connect(m_client, &QMqttClient::stateChanged, this, &Messenqt::updateStateChange); QObject::connect(m_client, &QMqttClient::connected, this, &Messenqt::subscribe_results); QObject::connect(m_client , &QMqttClient::messageReceived , this , &Messenqt::result_received); // connect(messqt->m_client, &QMqttClient::stateChanged, messqt, &Messenqt::updateLogStateChange); // connect(m_client, &QMqttClient::disconnected, this, &Publisher::disconnect); // connect(messqt->m_client, &QMqttClient::connected, messqt, &Messenqt::sendingMessage); msg_timer = new QTimer(this); connection_check = new QTimer(this); // m_client->subscribe(result_topic ,2); //TODO big chip save timer. connect(msg_timer, SIGNAL(timeout()) , this , SLOT(sendMessages())); msg_timer->start(4000); connect(connection_check , SIGNAL(timeout()) , this , SLOT(reconnect())); connection_check->start(5000); }
This thread started from the main GUI thread.
When started the app it says :
QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread
-
@RahibeMeryem said in qmqtt auto reconnect on disconnet:
can I connect signals from threads signal from it ?
What do you mean? Do you mean to connect a signal to a slot living in different threads? If so then yes you can (use Qt::QueuedConnection connection type). But that doesn't mean that what you're doing in other thread with your socket is allowed.
-
for example :
Main thread is Widget.cpp which is updating UI.
Messenqt.cpp is also a thread created by Widget.cpp with moveThread .
so how can I use
connect(m_client, &QMqttClient::connected, this, &Messenqt::subscribe_results);
in Messenqt ?
-
@RahibeMeryem You should do the connect in Widget.cpp and add Qt::QueuedConnection to the connect call
-
Whey there is not an autoconnect on diconnect is Qt qmqtt :) . There is in the pyhthon... is there any ?
-
@RahibeMeryem You should ask qmqtt project I guess
-
Looking into it, but I am confused qmqtt in thread and connecting signal slot inside that thread also to check connection and re-connect.
is there any minimalistic example for qmqtt running in a thread like:
mainwindow.cppmessenqt_thread = new QThread(); messqt = new Messenqt(); messqt->moveToThread(messenqt_thread); messenqt_thread->start();
Messenqt.cpp
Messenqt::Messenqt(QObject *parent) : QObject(parent) { // qRegisterMetaType<QMqttClient::ClientState>("QMqttClient::ClientState"); m_client = new QMqttClient(this); m_client->setHostname("xx.15.43.84"); m_client->setPort(1883); m_client->setClientId("001"); qDebug() << "?? is here" << m_client->state() << endl; if (m_client->state() == QMqttClient::Disconnected){ qDebug() << "state Disconnected 01" << endl;} else{qDebug() << "state Connected 01" << endl;}; connect(m_client, &QMqttClient::stateChanged, this, &Messenqt::updateLogStateChange); connect(m_client, &QMqttClient::disconnected, this, &Messenqt::brokerDisconnected); connect(m_client, &QMqttClient::connected, this, &Messenqt::succesfully_connected); connect(m_client, &QMqttClient::pingResponseReceived, this , &Messenqt::pingReceived); } void Messenqt::brokerDisconnected() { qDebug() << "Qmqt -- :: Disconnected ????****????*****???? " << endl; if (m_client->state() != 2){ qDebug() << " Re-connecting......" << endl ; qDebug ()<< " state : " << m_client->state() << endl; m_client->setHostname("xx.15.43.84"); m_client->setPort(1883); m_client->setClientId("RASPBERRY-009"); m_client->setUsername("xx"); m_client->setPassword("xx"); m_client->setKeepAlive(60); m_client->setCleanSession(false); m_client->connectToHost(); }
this is not working... I see debug message at the emqt server :
14:49:37.455 [debug] Client(78.187.191.133:50395): Terminated for {shutdown,closed}
-
@RahibeMeryem said in qmqtt auto reconnect on disconnet:
I am confused qmqtt in thread and connecting signal slot inside that thread also to check connection and re-connect
As I said you can connect signals slots across threads