laravel7 通过http协议控制mqtt并给mqtt协议发消息
想通过前端控制mqqt协议,但是必须通过后台去控制mqtt目前是后台是php框架用的laravel7
这里用到了 Workerman 官方文档
我这里会创建两个类 一个是发消息的 一个是接收消息的
发消息的 TestMqtt.php
收消息的 Subscribe.php
1.安装 Workerman
composer require workerman/workerman
2.安装 mqtt
composer require workerman/mqtt
3.通过 artisan 创建两个自定义的命令类
运行后就会在app\Console\Commands文件夹下生成一个自定义的类TestMqtt.php和Subscribe.php
php artisan make:command TestMqtt //发消息的类 php artisan make:command Subscribe //收消息的类

随后在app\Console\Kernel.php文件中$commands数组里添加一行刚刚生成的自定义类文件路径
\App\Console\Commands\TestMqtt::class, \App\Console\Commands\Subscribe::class

4.首先我们编辑收消息的类 Subscribe.php
<?php namespace App\Console\Commands; use Illuminate\Console\Command; use Workerman\Worker; use Workerman\Mqtt\Client; class Subscribe extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'start:s'; /** * The console command description. * * @var string */ protected $description = 'start subscribe'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return int */ public function handle() { $worker = new Worker(); $worker->onWorkerStart = function () { $mqtt = new Client('mqtt://127.0.0.1:1883'); $mqtt->onConnect = function ($mqtt) { $mqtt->subscribe('/result/out/hdl'); }; $mqtt->onMessage = function ($topic, $content) { $this->info('收到消息' . $content); $this->info("sub: $topic, $content"); }; $mqtt->connect(); }; Worker::runAll(); } }
5.编辑发消息的类 TestMqtt.php
<?php namespace App\Console\Commands; use Illuminate\Console\Command; use Workerman\Worker; use Workerman\Mqtt\Client; class TestMqtt extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'mqtt {start}'; /** * The console command description. * * @var string */ protected $description = 'mqtt start'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return int */ public function handle() { $worker = new Worker(); $http_port = '8898'; $worker->onWorkerStart = function ($worker) use ($http_port) { $ws_worker = new Worker('http://0.0.0.0:' . $http_port); $ws_worker->onMessage = function ($connection, $request) { $id = $request->post('id') ?? 0; $topic = '/result/out/hdl';//监听的主题 $port = '1883';//mqtt端口 $mqtt_ip = '127.0.0.1';//mqttip //具体options参数可以到官网去查看 https://www.workerman.net/doc/workerman/components/workemran-mqtt.html $options = [ 'username' => 'chunge', 'password' => '1234', // 'debug' => true, ]; $clicke = "mqtt://$mqtt_ip:$port"; $mqtt = new Client($clicke, $options); $json_encode = json_encode(array('id' => $id)); $mqtt->onConnect = function ($res) use ($json_encode, $topic, $mqtt, $connection) { $this->info('http向mqqtt发送消息:' . $json_encode); $res->publish($topic, $json_encode); $mqtt->disconnect(); $message = tcpSuccess(); $connection->send($message); }; $mqtt->connect(); $mqtt->onError = function (\Exception $e) use ($mqtt, $connection) { $this->error('请先启动mqtt服务:' . $e->getMessage()); $mqtt->disconnect(); $message = tcpError('请先启动mqtt服务:' . $e->getMessage()); $connection->send($message); }; $mqtt->onClose = function () { $this->error('断开连接'); }; }; $worker->ws_worker = $ws_worker; $ws_worker->listen(); }; /** * http发送成功反馈 */ function tcpSuccess($message = 'ok') { $res = array( 'status' => 0, 'message' => $message, 'data' => [], 'attache' => [] ); return json_encode($res); } /** * http发送失败反馈 */ function tcpError($message = '账户或密码错误') { $res = array( 'status' => 9001, 'message' => $message, 'data' => [], 'attache' => [] ); return json_encode($res); } // 运行worker Worker::runAll(); } }
6.将两个类在项目的更目录打开cmd输入以下命令运行起来
//先启动收消息的类 php artisan start:s //最后启动发消息的类 php artisan mqtt start
7.那么接下来就是见证奇迹的时刻
通过postman 已post方式请求发送了id值为989

————————————————
版权声明:本文为CSDN博主「极客铁憨憨」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_42985794/article/details/121654179
转载:https://baijiahao.baidu.com/s?id=1718030420914483451&wfr=spider&for=pc

浙公网安备 33010602011771号