MQTT 订阅问题

crazyday7 2019-05-28 04:27:30
我是参考 https://github.com/bluerhinos/phpMQTT
执行范例接收MQTT没问题,但改用CodeIgniter去接收就不行
请问要怎么改才能运作?

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

include_once(__DIR__ . '/../third_party/phpMQTT.php');

class MQTT_subscribe extends CI_Controller {

public function __construct()
{
parent::__construct();
}

public function MQTTSubscribe()
{
$server = "IP"; // change if necessary
$port = 1883; // change if necessary
$username = "XXX"; // set your username
$password = "XXX"; // set your password
$client_id = "phpMQTT-subscriber"; // make sure this is unique for connecting to sever - you could use uniqid()

$mqtt = new phpMQTT($server, $port, $client_id);

if(!$mqtt->connect(true, NULL, $username, $password)) {

goto err_out;
}

$topics['bluerhinos/phpMQTT/examples/publishtest'] = array("qos" => 0, "function" => "procmsg");

$mqtt->subscribe($topics, 0);

while($mqtt->proc()){

}

$mqtt->close();

err_out:
return ;
}

function procmsg($topic, $msg){
//echo "Msg Recieved: " . date("r") . "\n";
//echo "Topic: {$topic}\n\n";
//echo "\t$msg\n\n";

err_out:
return $msg;
}
}
...全文
279 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
crazyday7 2019-05-30
  • 打赏
  • 举报
回复
有放log到phpMQTT.php,function有被呼叫 但function procmsg沒被呼叫 并没有提示错误
kang#kang 2019-05-28
  • 打赏
  • 举报
回复
ci中的引入方式 不是这种吧 就想tp 有 vendor 函数一样 你引入后 实力化的时候 也是 $object = new \Model(); 这种方式 具体的你占一下错误信息把

21,893

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧