Mosquitto-PHP
Mosquitto-PHP copied to clipboard
OnConnect function does not get the status of the mosquito server
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?
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 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。
Hi @mgdm !
Did you some change to get error code from Mosquitto?
Best regards.
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