namespace app\api\controller;
use think\Log;
use Workerman\Mqtt\Client;
use Workerman\Worker;
class Subscribe{
public function index()
{
$worker = new Worker();
$worker->onWorkerStart = function(){
$keepalive = 90;
$cleanSession = true;
$options = [
'username' => '******',
'password' => '******',
'keepalive' => $cleanSession,
'clean_session' => $keepalive,
'client_id' => 'workerman-mqtt',
'debug' => true,
];
$mqtt = new Client('mqtt://127.0.0.1:1883',$options)
$mqtt->onConnect = function($mqtt) {
$mqtt->subscribe('$SYS/brokers/+/clients/+/+');
};
$mqtt->onMessage = function($topic, $content){
$this->onMessage($topic, $content);
};
$mqtt->connect();
};
Worker::runAll();
}
function onMessage($topic,$msg)
{
$to_pic = explode('/',$topic);
$device_id = isset($to_pic[4]) ? $to_pic[4] : '';
$event = isset($to_pic[5]) ? $to_pic[5] : '';
if (strlen($device_id) == 14){
var_dump('设备id:'.$device_id);
var_dump('操作:'.$event);
}
}
}
namespace app\api\command;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Request;
class Stomp extends Command {
protected function configure() {
// 进程名,下文命令行中会体现
$this->setName('subscribe')->setDescription('订阅Stomp');
}
protected function execute(Input $input, Output $output) {
// 被执行代码所在模块
\Request()->module("api");
// 控制器和方法
$output->writeln(controller('api/subscribe')->index());
}
}
'app\api\command\Stomp',
php think subscribe
Usage: php yourfile [mode]
Commands:
start Start worker in DEBUG mode.
Use mode -d to start in DAEMON mode.
stop Stop worker.
Use mode -g to stop gracefully.
restart Restart workers.
Use mode -d to start in DAEMON mode.
Use mode -g to stop gracefully.
reload Reload codes.
Use mode -g to reload gracefully.
status Get worker status.
Use mode -d to show live status.
connections Get worker connections.
require __DIR__ . '/../vendor/autoload.php';
use Workerman\Mqtt\Client;
use Workerman\Worker;
$worker = new Worker();
$worker->onWorkerStart = function(){
$keepalive = 90;
$cleanSession = true;
$options = [
'username' => '*****',
'password' => '*****',
'keepalive' => $cleanSession,
'clean_session' => $keepalive,
'client_id' => 'workerman-mqtt',
'debug' => false,
];
$mqtt = new Client('mqtt://127.0.0.1:1883',$options);
$mqtt->onConnect = function($mqtt) {
$mqtt->subscribe('$SYS/brokers/+/clients/+/+');
};
$mqtt->onMessage = function($topic, $content){
var_dump($topic,$content);
};
$mqtt->connect();
};
Worker::runAll();
mqtt://127.0.0.1:1883
这个ip是不是要改变?
你这明显有问题,
你贴图的第17行需要将 'debug' => false, 改成 'debug' => “false”,
补充: 题主多关注一下php新版本的语法有变化的地方
问题已自行解决,修改了插件中的 worker.php 文件