mgdm / Mosquitto-PHP

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

loopforever causes issue when ble gateway is not connected #107

Open nishitmodi2591 opened 5 years ago

nishitmodi2591 commented 5 years ago

The code works fine when the BLE gateway is on. Whenever the BLE gateway is off it stops working and doesn't even give connection error.

Here is my code:

try {
    $mqttclient = new \Mosquitto\Client;

    $mqttclient->onConnect(function() use ($mqttclient, $gateway_topic)
    {
        $mqttclient->subscribe($gateway_topic, 0);
    });

    $mqttclient->onMessage(function($message) use($mqttclient)
    {
        $this->DataParse($message);
    });

    if(!empty($username) && !empty($password))
    {
        $mqttclient->setCredentials($username, $password);
    }

    $mqttclient->connect($mqtt_server_host, $mqtt_server_port, 60);

    $mqttclient->loopforever();

} catch (Exception $e) {
    writeException($e);
}

Please suggest how it can be resolved.