emqx / qmqtt

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

Connecting to URL (HiveMQ) #216

Closed Saeedsaeb closed 3 years ago

Saeedsaeb commented 3 years ago

Hi , i am quite new to CPP and Qt so sorry if i am basic i am trying to connect to HiveMQ using TCP on 1883 , with no success i did get connect to local host but it seems that QHostAddress dose not get Url , and only IP as String , i tried to read the source , couldn't find any constructor for URL in TCP (Websocket is there)

this is a basic code that i thought it would work

QHostAddress ad = QHostAddress("broker.hivemq.com"); QMQTT::Client client = new QMQTT::Client(ad, 1883); client->setClientId("clientId"); client->setUsername("user"); client->setPassword("password"); client->setWillQos(2); client->setKeepAlive(65000); client->connectToHost(); if(client->isConnectedToHost()) { QMessageBox Message; Message.setText("CONNECTED"); Message.setWindowTitle("OK"); Message.addButton(QMessageBox::StandardButton::Ok); Message.exec(); } const char stdString ="SALAM"; QByteArray byteArray(stdString,sizeof(stdString)); QMQTT::Message msg = QMQTT::Message(100, "SAEED13",byteArray); msg.setTopic("SAEED13"); msg.setPayload(QByteArray("HII !!!")); client->publish(msg);

any help would be appreciated in advance

ejvr commented 3 years ago

I see 2 issues in your code. First one is (as you already stated) that you cannot use a hostname (like broker.hivemq.com) to create a QHostAddress object. To use a hostname you need this constructor:

    Client(const QString& hostName,
           const quint16 port,
           const bool ssl,
           const bool ignoreSelfSigned,
           QObject* parent = NULL);

Since you are using port 1883, you're probably not using SSL, so put the ssl and ignoreSelfSigned parameters to false.

Second issue is that you should wait for the connection to be established before publishing and subscribing. The connectToHost function will only start the connection process, it will not wait for it. That's why in your code the isConnectedToHost function will always return false. In order to fix this, it's best to connect to the connected signal of the Client class and start publishing from there. Please check the example project in the QMQTT repo for more information on how to do that.

Saeedsaeb commented 3 years ago

I see 2 issues in your code. First one is (as you already stated) that you cannot use a hostname (like broker.hivemq.com) to create a QHostAddress object. To use a hostname you need this constructor:

    Client(const QString& hostName,
           const quint16 port,
           const bool ssl,
           const bool ignoreSelfSigned,
           QObject* parent = NULL);

Since you are using port 1883, you're probably not using SSL, so put the ssl and ignoreSelfSigned parameters to false.

Second issue is that you should wait for the connection to be established before publishing and subscribing. The connectToHost function will only start the connection process, it will not wait for it. That's why in your code the isConnectedToHost function will always return false. In order to fix this, it's best to connect to the connected signal of the Client class and start publishing from there. Please check the example project in the QMQTT repo for more information on how to do that.

Thank you so much !! that was amazing , I did look at other constructors and add a button which when i click , it will publish to HiveMQ , everything works perfectly , Amazing library ,