Reconnection Issue
Tommybearrr opened this issue · comments
Tommybearrr commented
I am struggling to get the client lib to reconnect.
What is the correct sequences of methods to use to disconnect a client, if connection is lost and reconnect the client again to the broker?
Here is an example of code I have been playing around with?
` GeyserStatus thestate;
MQTTMessage message;
int rc=0;
char payload[1000];
MQTTClient Newclient;
message.qos = 1;
message.retained = 0;
message.payload = payload;
sprintf(payload, "%s", SendMessage);
message.payloadlen = strlen(SendMessage);
if(ReconnectMQTT)
{
if(ReconnectCounter++ >= 10)
{
NetworkInit(&network);
if (0!= ConnectNetwork(&network, (char*)AZURE_HUB_URL, MQTT_BROKER_PORT,0)){
return;
}
MQTTClientInit(&Newclient, &network, 10000, sendbuf, sizeof(sendbuf), readbuf, sizeof(readbuf));
connectData.MQTTVersion = 4; //use protocol version 3.1.1
connectData.clientID.cstring = DEVICE_ID;
connectData.username.cstring = MQTT_USER_NAME;
if ((rc = MQTTConnect(&Newclient, &connectData)) != 0){
return;
}
if ((rc = MQTTSubscribe(&Newclient, MQTT_SUB_TOPIC, &subQoS, MQTTmessageCallback)) != 0){
return;
}
ReconnectMQTT = false;
ReconnectCounter = 0;
client = Newclient;
}
return;
}
if ((rc = MQTTPublish(&client, MQTT_PUB_TOPIC, &message)) != 0);
{
if(MessageFailures++> 3)
{
MessageFailures = 0;
MQTTDisconnect(&client);
ReconnectMQTT = true;
ReconnectCounter = 10;
}
}
printf("ERR>> Return code from MQTT publish is %d\n\r", rc);
printf("> send message: %s\r\n",SendMessage );`
Would appreciate if anyone can point me in the right direction.
Thank you