philbowles / PangolinMQTT

PangolinMQTT - ArduinoIDE client library for ESP8266, ESP32 and STM32-NUCLEO
Other
71 stars 21 forks source link

How to correctly check if connection still available or not? #30

Closed wladwnt closed 3 years ago

wladwnt commented 3 years ago

(Pangolin release 1.0.0) How to correctly check if connection still available or not? I tryied to set/reset a bool variable (flag) in cbOnConnect/cbOnDisconnect callback function. If this flag is false for longer than 20 seconds, I try to connect again. But very seldom (2 times so far) it happens (after many many hours) that flag is false and if I try connect, then nothing happens. Looking in the code in connect() function the first check is: if(PANGO::TCP) return; I suppose that what I have, my flag is false but TCP connection still there. In that cases I can see in MQTT server (Mosquitto) log "Socket error on client esp32-xxxx, disconnecting.".

So, whould the best way not to use own flag and set/reset it in callback functions, but just check PANGO::TCP ?

philbowles commented 3 years ago

The connection checking has changed in 1.0.1 - see if the new code solves your problem. Having said that, if you need to check for connection in the first place, maybe you are doing something wrong? The "best" way is to structure your code so it runs when there is a connection and doesn't run when there isn't, by using the onConnect / onDisconnect callbacks.

Providing those work correctly (hoping 1.0.1 has fixed any issues) then you should never need to explicitly check for a connection