Skip to content

Commit

Permalink
Delete existing connection before connecting
Browse files Browse the repository at this point in the history
If the current connection is terminated unexpectedly (e.g. because the
WiFi connection is dropped) the disconnect callback is not called. This
means that reestablishing the existing connection is not possible any
more.

Avoid this issue by forcefully removing the existing connection before
initiating the reconnection. Calling MQTT_Disconnect and waiting for
the disconnection callback should still be performed for intentional
disconnections.

Fixes #96
  • Loading branch information
lorenzph committed Apr 3, 2016
1 parent 670cfd7 commit 0f980bf
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions mqtt/mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -751,10 +751,11 @@ MQTT_InitLWT(MQTT_Client *mqttClient, uint8_t* will_topic, uint8_t* will_msg, ui
void ICACHE_FLASH_ATTR
MQTT_Connect(MQTT_Client *mqttClient)
{
// Do not connect if this client is already connected otherwise the
// two espconn connections may interfere causing unexpected behaviour.
if (mqttClient->pCon) {
return;
// Clean up the old connection forcefully - using MQTT_Disconnect
// does not actually release the old connection until the
// disconnection callback is invoked.
mqtt_tcpclient_delete(mqttClient);
}
mqttClient->pCon = (struct espconn *)os_zalloc(sizeof(struct espconn));
mqttClient->pCon->type = ESPCONN_TCP;
Expand Down

0 comments on commit 0f980bf

Please sign in to comment.