当我从cloudmqtt获取数据时,我遇到了一个问题。我已经从这个链接GitHub下载了项目。
subscribe.php
在我的subscribe.php文件中,从phpMQTT.php文件调用了一个名为proc的函数。这是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;
}如果我在phpMQTT.php文件的顶部使用set_limit_time(0);。然后,当我在浏览器中浏览subscribe.php时,它永远不会结束加载。
如果我在phpMQTT.php文件的顶部使用set_limit_time(60);。然后,当我在60秒后在浏览器中浏览subscribe.php时,我得到了一些数据(6个数据),并出现了这个错误。
致命错误:在第275行的C:\xampp\htdocs\phpMQTT\examples\phpMQTT.php中超过了60秒的最大执行时间
第275行表示usleep(100000);。
如果我在phpMQTT.php文件的顶部使用set_limit_time(30);。然后,当我在30秒后在浏览器中浏览subscribe.php时,我得到了一些数据(3个数据),并出现了这个错误。
致命错误:在第275行的C:\xampp\htdocs\phpMQTT\examples\phpMQTT.php中超过了30秒的最大执行时间
我哪里错了?我该如何解决这个问题呢?
发布于 2018-05-31 19:43:13
proc方法被设计为“永远”运行
while($mqtt->proc()){ }因为它总是返回true,所以循环永远不会结束。因此,如果您将时间限制设置为30s (或其他任何值),则在此时间段之后,它将始终失败(最有可能是在usleep方法中,因为脚本几乎花费了所有的时间)
正如在一些(github issue)中提到的-订阅应该直接在服务器后台运行,而不是通过浏览器调用(因此您可以避免服务器超时,浏览器连接超时,等等)
https://stackoverflow.com/questions/50622605
复制相似问题