emqx / qmqtt

MQTT client for Qt
https://www.emqx.com
Other
692 stars 337 forks source link

Auto reconnect tries to reconnect even though a connection has been established through `connectToHost()` #227

Closed benthillerkus closed 2 years ago

benthillerkus commented 3 years ago

This causes this exception: QAbstractSocket::connectToHost() called when already looking up or connecting/connected to "192.168.0.5".

I'm trying to build a GUI where the user can configure host / port / usernam / password. Upon saving, the new settings will be applied to the QMQTT::Client. If the user typed in wrong information, the connection will fail and autoreconnect will try to reestablish the connection. Unfortunately, auto reconnect will not notice when the user corrects the information and will keep on trying to connect with the old settings. To mitigate this, when the user changes settings, I disable the auto reconnect, call the setters on the client, call connectToHost, and then enable auto reconnect again, once a connection has been established, but even that doesn't seem to work.

ejvr commented 3 years ago

The easiest way to get this right is to create a new QMQTT::client when the user changes the configuration. This may increase the overhead, but it will not be happen very often, and it will become a necessity when you allow the user to switch between connection type (plain TCP / SSL / Websocket), because only the constructor of QMQTT::client will create the right substructure.

A few years ago I created a small example project to illustrate how to create a connection to a mqtt broken in a (qml) UI. If you're interested, you can find it here. The interesting part is in the function QmlQmqttClient::connectToHost().