iot-admin/app/mqtt/Publish.php

54 lines
1.3 KiB
PHP
Raw Permalink Normal View History

2023-11-30 22:53:01 +08:00
<?php
2023-12-01 10:30:07 +08:00
namespace app\mqtt;
2023-12-01 16:54:26 +08:00
use Workerman\Connection\TcpConnection;
/**
* 发送
*/
2023-11-30 22:53:01 +08:00
class Publish
{
2023-12-01 16:54:26 +08:00
public function onConnect(TcpConnection $connection)
2023-11-30 22:53:01 +08:00
{
2023-12-20 19:20:05 +08:00
$mqtt = new \Workerman\Mqtt\Client('mqtt://mqtt.lihaink.cn:1883', array(
"username" => "lihai_lot_land_1",
"password" => "lihai_lot_land_1",
2023-12-01 16:54:26 +08:00
"client_id" => "admin_123",
2023-11-30 22:53:01 +08:00
));
$mqtt->connect();
2023-12-01 16:54:26 +08:00
$connection->mqtt = $mqtt;
}
/**
* {"topic":"demo","content":"asdasd"}
*/
public function onMessage(TcpConnection $connection, $data)
{
$data = json_decode($data, true);
if ($data == null) {
$connection->send("参数不能为空");
return false;
}
if ($data['topic'] == '') {
$connection->send("topic为空");
return false;
}
if ($data['content'] == '') {
$connection->send("content为空");
return false;
}
$topic = $data['topic'];
$content = $data['content'];
$res = $connection->mqtt->publish($topic, $content);
if ($res == null) {
$connection->send("发布成功");
} else {
$connection->send("发布失败");
}
2023-11-30 22:53:01 +08:00
}
2023-12-01 16:54:26 +08:00
}