Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OnConnect function does not get the status of the mosquito server #58

Open
flylan opened this issue Oct 29, 2016 · 4 comments
Open

OnConnect function does not get the status of the mosquito server #58

flylan opened this issue Oct 29, 2016 · 4 comments

Comments

@flylan
Copy link

flylan commented Oct 29, 2016

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?

@mgdm
Copy link
Owner

mgdm commented Oct 29, 2016

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.

@flylan
Copy link
Author

flylan commented Oct 31, 2016

@mgdm
http:https://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。

@FernandoGarcia
Copy link

Hi @mgdm !

Did you some change to get error code from Mosquitto?

Best regards.

@amigian74
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants