想通过前端控制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
{
protected $signature = 'start:s';
protected $description = 'start subscribe';
public function __construct()
{
parent::__construct();
}
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
{
protected $signature = 'mqtt {start}';
protected $description = 'mqtt start';
public function __construct()
{
parent::__construct();
}
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_ip = '127.0.0.1';
$options = [
'username' => 'chunge',
'password' => '1234',
];
$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();
};
function tcpSuccess($message = 'ok')
{
$res = array(
'status' => 0,
'message' => $message,
'data' => [],
'attache' => []
);
return json_encode($res);
}
function tcpError($message = '账户或密码错误')
{
$res = array(
'status' => 9001,
'message' => $message,
'data' => [],
'attache' => []
);
return json_encode($res);
}
Worker::runAll();
}
}
6.将两个类在项目的更目录打开cmd输入以下命令运行起来
php artisan start:s
php artisan mqtt start
7.那么接下来就是见证奇迹的时刻
通过postman 已post方式请求发送了id值为989
|