Skip to content

Commit df82256

Browse files
committed
disconnect client when network fails
1 parent 915947c commit df82256

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

src/MQTTClient.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ void MQTTClient::unsubscribe(const char * topic) {
103103
}
104104

105105
void MQTTClient::loop() {
106+
if(!this->network.connected() && this->client->isConnected()) {
107+
// the following call will not send a packet but reset the instance
108+
// to allow proper reconnection
109+
this->client->disconnect();
110+
}
111+
106112
this->client->yield();
107113
}
108114

src/Network.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ int Network::write(unsigned char* buffer, int len, int timeout) {
3030
client->setTimeout(timeout);
3131
return client->write((uint8_t*)buffer, len);
3232
}
33+
34+
boolean Network::connected() {
35+
return client->connected();
36+
}
3337

3438
int Network::disconnect() {
3539
client->stop();

src/Network.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ class Network {
88
int connect(char* hostname, int port);
99
int read(unsigned char* buffer, int len, int timeout);
1010
int write(unsigned char* buffer, int len, int timeout);
11+
boolean connected();
1112
int disconnect();
1213
};

0 commit comments

Comments
 (0)