1

I have faced a problem when I get data from cloudmqtt. I have downloaded project from this link GitHub.

subscribe.php

In my subscribe.php file a function name proc called from phpMQTT.php file. This is subscribe.php file code

$topics['sensor_data'] = array("qos" => 0, "function" => "procmsg");
$mqtt->subscribe($topics, 0);
while($mqtt->proc()){       
}
$mqtt->close();

phpMQTT.php

In my phpMQTT.php file a function proc defined like below.

function proc( $loop = true){
    if(1){
        $sockets = array($this->socket);
        $w = $e = NULL;
        $cmd = 0;

            //$byte = fgetc($this->socket);
        if(feof($this->socket)){
            if($this->debug) echo "eof receive going to reconnect for good measure\n";
            fclose($this->socket);
            $this->connect_auto(false);
            if(count($this->topics))
                $this->subscribe($this->topics);    
        }

        $byte = $this->read(1, true);

        if(!strlen($byte)){
            if($loop){
                usleep(100000); //Fatal error shows this line
            }

        }else{ 

            $cmd = (int)(ord($byte)/16);
            if($this->debug) echo "Recevid: $cmd\n";

            $multiplier = 1; 
            $value = 0;
            do{
                $digit = ord($this->read(1));
                $value += ($digit & 127) * $multiplier; 
                $multiplier *= 128;
                }while (($digit & 128) != 0);

            if($this->debug) echo "Fetching: $value\n";

            if($value)
                $string = $this->read($value);

            if($cmd){
                switch($cmd){
                    case 3:
                        $this->message($string);
                    break;
                }

                $this->timesinceping = time();
            }
        }

        if($this->timesinceping < (time() - $this->keepalive )){
            if($this->debug) echo "not found something so ping\n";
            $this->ping();  
        }


        if($this->timesinceping<(time()-($this->keepalive*2))){
            if($this->debug) echo "not seen a package in a while, disconnecting\n";
            fclose($this->socket);
            $this->connect_auto(false);
            if(count($this->topics))
                $this->subscribe($this->topics);
        }
    }
    return 1;
}

If I use set_limit_time(0); in top of the phpMQTT.php file. Then when I brows subscribe.php in browser it never ends loading.

If I use set_limit_time(60); in top of the phpMQTT.php file. Then when I brows subscribe.php in browser after 60 seconds later I get some data(6 data) with this error.

Fatal error: Maximum execution time of 60 seconds exceeded in C:\xampp\htdocs\phpMQTT\examples\phpMQTT.php on line 275

Line 275 indicate that usleep(100000);.

If I use set_limit_time(30); in top of the phpMQTT.php file. Then when I brows subscribe.php in browser after 30 seconds later I get some data(3 data) with this error.

Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\phpMQTT\examples\phpMQTT.php on line 275

Where I am wrong? And how can I fix this problem?

A.A Noman
  • 4,233
  • 9
  • 23
  • 38

1 Answers1

1

The proc method is designed to run "forever"

while($mqtt->proc()){ }

Because it returns always true the loop never ends. So if you set time limit to 30s (or anything else) it will always fail after this time period (most likely in the usleep method because the script spends nearly all of the time there)

As mentioned in some (github issue) - the subscribe should run on the server in background directly instead of beeing called via browser (so you avoid server-timeouts, browser-connection timeouts, ...)

Evil_skunk
  • 2,255
  • 2
  • 24
  • 30
  • 1
    run your script directly on the server https://secure.php.net/manual/en/features.commandline.usage.php and in the script do whatever you want with the received data, e.g. write to a file / database, send it to somewhere else, ... – Evil_skunk May 31 '18 at 12:44