Mosquitto-PHP icon indicating copy to clipboard operation
Mosquitto-PHP copied to clipboard

OnConnect function does not get the status of the mosquito server

Open flylan opened this issue 9 years ago • 4 comments
trafficstars

Here is the code

<?php $client = new Mosquitto\Client(); $client->onConnect(function ($rc,$message) { print_r($rc); }); try { $client->connect("127.0.0.1", 1883); $client->subscribe('test', 1); while(true) { $client->loopForever(); sleep(2); } } catch (Mosquitto\Exception $e) { print_r($e); }

When the mosquito server is not available, the connect method throws a Mosquitto\Exception. But the onConnect callback function can not get codes Response. So how do I get the status of the mosquito server?

flylan avatar Oct 29 '16 13:10 flylan

You can get the error message from the exception:

<?php
try {
    $c = new Mosquitto\Client();
    $c->connect('localhost', 1234);
} catch (Mosquitto\Exception $e) {
    var_dump($e->getCode());
    var_dump($e->getMessage());
}
int(0)
string(18) "Connection refused"

However you'll note that the error code is always 0. I can fix that and put the actual Mosquitto error code in there.

mgdm avatar Oct 29 '16 18:10 mgdm

@mgdm http://mosquitto-php.readthedocs.io/en/latest/client.html

However, in the description of the onConnect method, there is a return status code which is not available for the server. I think the state of the mqtt server is not available, written in the onDisconnetc callback method which will be better. Because the server is not available, also belong to the event of disconnection。

flylan avatar Oct 31 '16 02:10 flylan

Hi @mgdm !

Did you some change to get error code from Mosquitto?

Best regards.

FernandoGarcia avatar Jul 22 '17 16:07 FernandoGarcia

Hi @mgdm !

I'v to ask the same question. Are there any changes made or planned, because there is no chance to recognize any connection error. "connect" is not throwning any exception and if the broker is unreachable there is no message in onConnect neither onDisconnect. So it's quiet unusable.

Best regards and an answer would be appreciated

amigian74 avatar Oct 17 '17 07:10 amigian74