Skip to content

Commit

Permalink
Merge pull request #8 from cakesifu/add-disconnect
Browse files Browse the repository at this point in the history
Add implementation for disconnect callback
  • Loading branch information
tuanpmt authored Nov 10, 2017
2 parents 295dc65 + a7d974d commit efb28eb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/ESP8266MQTTClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ void MQTTClient::onConnect(THandlerFunction fn)
{
_connected_cb = fn;
}
void MQTTClient::onDisconnect(THandlerFunction fn)
{
_disconnected_cb = fn;
}
void MQTTClient::onSubscribe(THandlerFunction_PubSub fn)
{
_subscribe_cb = fn;
Expand All @@ -283,13 +287,19 @@ void MQTTClient::handle(void)
if(!_initialized)
return;
if(!connected()) {
if (!_disconnect_cb_called) {
_disconnected_cb();
_disconnect_cb_called = true;
}

if(_reconnect_tick != 0 && millis() - _reconnect_tick < MQTT_RECONNECT_TIMEOUT)
return;

_reconnect_tick = millis();
if(connect()) {
if(_connected_cb)
_connected_cb();
_disconnect_cb_called = false;
_keepalive_tick = millis();
} else {
return;
Expand Down
1 change: 1 addition & 0 deletions src/ESP8266MQTTClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ class MQTTClient
int _clean_session;
int _keepalive;
bool _initialized;
bool _disconnect_cb_called;

THandlerFunction _connected_cb;
THandlerFunction _disconnected_cb;
Expand Down

0 comments on commit efb28eb

Please sign in to comment.