mgdm / Mosquitto-PHP

A wrapper for the Eclipse Mosquitto™ MQTT client library for PHP.
BSD 3-Clause "New" or "Revised" License
533 stars 147 forks source link

Throwing an exception instead of reconnecting #42

Open bitfreak25 opened 8 years ago

bitfreak25 commented 8 years ago

As I tested reconnecting by using loopForever() and restarting the local mqtt-broker mosquitto, my script stops. The output was:

PHP Fatal error: Uncaught exception 'Mosquitto\Exception' with message 'The connection was lost.' in /home/user/public_html/test.php:41 Stack trace:

0 /home/user/public_html/test.php(41): Mosquitto\Client->loopForever()

1 {main}

thrown in /home/user/public_html/test.php on line 41

I expected a reconnect, but this wasn't the case. Whats wrong here?

abelkbil commented 8 years ago

@bitfreak25 Hello sir, How did you solved this issue ?

mgdm commented 8 years ago

I used to use the mosquitto_loop_forever() function from the underlying library to handle the reconnects, but unfortunately that means that the function never returns at all, so if you attempt to break out of the loop, throw an exception, or similar, PHP doesn't know about it. That means I need to reimplement the function myself, which I've done, but I didn't get to implementing all of the reconnection logic. That's on my to-do list, and I hope to have it ready to look at in the next few days. In the mean time I'm afraid you need to do it the long way in your own code.

bitfreak25 commented 8 years ago

I solved it by using a php-restart-script similar to an answer on stackoverflow. But for me this looks like a workaround, so it would be nice, if this will be implemented in the library itself.