1

我从 cloudmqtt 获取数据时遇到了问题。我已经从这个链接GitHub下载了项目。

订阅.php

在我的文件中,从文件中调用subscribe.php了一个函数名procphpMQTT.php。这是subscribe.php文件代码

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

phpMQTT.php

在我的phpMQTT.php文件中,定义如下的函数proc 。

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;
}

如果我set_limit_time(0);在 phpMQTT.php 文件顶部使用。然后,当我subscribe.php在浏览器中浏览时,它永远不会结束加载。

如果我set_limit_time(60);在 phpMQTT.php 文件顶部使用。然后,当我subscribe.php在 60 秒后浏览浏览器时,我得到一些数据(6 个数据)并出现此错误。

致命错误:第 275 行的 C:\xampp\htdocs\phpMQTT\examples\phpMQTT.php 中的最大执行时间超过了 60 秒

第 275 行表明usleep(100000);.

如果我set_limit_time(30);在 phpMQTT.php 文件顶部使用。然后,当我subscribe.php在 30 秒后浏览浏览器时,我得到一些数据(3 个数据)并出现此错误。

致命错误:第 275 行的 C:\xampp\htdocs\phpMQTT\examples\phpMQTT.php 中的最大执行时间超过 30 秒

我哪里错了?我该如何解决这个问题?

4

1 回答 1

1

proc方法旨在“永远”运行

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

因为它总是返回 true,所以循环永远不会结束。因此,如果您将时间限制设置为 30 秒(或其他任何时间),则在此时间段之后它总是会失败(很可能在usleep方法中,因为脚本几乎所有时间都花在那里)

正如一些(github问题)中提到的 - 订阅应该直接在后台运行在服务器上,而不是通过浏览器调用(这样你就可以避免服务器超时,浏览器连接超时,......)

于 2018-05-31T11:43:13.277 回答