bluerhinos / phpMQTT

a simple php class to connect/publish/subscribe to a MQTT broker
Other
773 stars 460 forks source link

subscribeAndWaitForMessage timeout - enhancement #119

Open lipflip opened 3 years ago

lipflip commented 3 years ago

It would be great to have some timeout option, otherwise you have an endless loop,

this is not ok in some situations.

danieleliberti commented 2 years ago
public function subscribeAndWaitForMessage($topic, $qos): string
    {
        $this->subscribe(
            [
                $topic => [
                    'qos' => $qos,
                    'function' => '__direct_return_message__'
                ]
            ]
        );

        $microtimeStart = microtime(true);
        do {
            $return = $this->proc();
            $microtime = microtime(true);

            if (($microtime - $microtimeStart) > 30  ){
                $return = '{"rtErr":"Time exceeded !"}';
            }
        } while ($return === true);
        return $return;
    }