【类 型】:test

【主	题】:
【描	述】:
	[原因]:修改测试页面 去掉compouser的库引用  删掉nginx的配置文件改用apache
	[过程]:
	[影响]:
【结	束】

# 类型 包含:
# feat:新功能(feature)
# fix:修补bug
# docs:文档(documentation)
# style: 格式(不影响代码运行的变动)
# refactor:重构(即不是新增功能,也不是修改bug的代码变动)
# test:增加测试
# chore:构建过程或辅助工具的变动
This commit is contained in:
tk 2024-06-11 17:05:08 +08:00
parent 414b394b6b
commit 69545500dd
2 changed files with 1 additions and 40 deletions

View File

@ -3,50 +3,11 @@
namespace Home\Controller;
use Think\Controller;
use PhpMqtt\Client\MqttClient;
use PhpMqtt\Client\Exceptions\MqttClientException;
use PhpMqtt\Client\ConnectionSettings;
class IndexController extends Controller
{
public function index()
{
//$this->display();
$server = 'www.szdot.top'; // MQTT 代理地址
$port = 1883; // MQTT 代理端口
$clientId = 'php-mqtt-client'; // 客户端 ID
$username = 'admin'; // 用户名(如果需要)
$password = '123456'; // 密码(如果需要)
try {
// 创建连接设置对象
$settings = (new ConnectionSettings)
->setUsername($username)
->setPassword($password);
// 实例化 MQTT 客户端
$mqtt = new MqttClient($server, $port, $clientId);
// 连接到 MQTT 代理
$mqtt->connect($settings);
// 检查是否连接成功
if ($mqtt->isConnected()) {
echo "Connected to MQTT broker successfully\n";
// 发布消息到指定的主题
$mqtt->publish('demo', 'Hello MQTT', 0);
echo "Message published successfully\n";
// 断开连接
$mqtt->disconnect();
echo "Disconnected from MQTT broker\n";
} else {
echo "Could not connect to MQTT broker\n";
}
} catch (MqttClientException $e) {
echo "Could not connect to MQTT broker: " . $e->getMessage();
}
$this->display();
}
}

View File