Connect Method
Connects to the MQTTSN Gateway.
Syntax
int Connect(const QString& qsHost, int iPort);
Remarks
This method connects to the MQTTSN Gateway by sending a CONNECT packet. Calling this method is equivalent to setting the RemoteHost property to Host, setting RemotePort to Port, and then setting the Connected property to True.
When connecting to an MQTT gateway, the class sends the following information:
- The values of the ClientId, CleanSession, and KeepAliveInterval properties.
- The values of the WillTopic and WillMessage properties and the WillRetain and WillQOS configuration settings.
Refer to CleanSession for more information about MQTT sessions; refer to WillTopic, WillMessage, WillQOS and WillRetain for more information about MQTT Wills.
Basic Connection Example
mqttsn1.ClientId = "testClient";
mqttsn1.CleanSession = true;
mqttsn1.KeepAliveInterval = 30;
mqttsn1.WillTopic = "wills/" + mqttsn1.ClientId;
mqttsn1.WillMessage = mqttsn1.ClientId + " was disconnected ungracefully!";
mqttsn1.Connect(host, port);
Error Handling
This method returns a result code; 0 indicates success, while a non-zero error code indicates that this method encountered an error during its execution. If an error occurs, the GetLastError() method can be called to retrieve the associated error message. (Note: This method's result code can also be obtained by calling the GetLastErrorCode() method after it returns.)