发布消息
<?php
require __DIR__ . '/vendor/autoload.php';
use Workerman\Worker;
$worker = new Worker();
$worker->onWorkerStart = function () {
$qos = 0;
$topic = 'test';
$port = 8883;
$keepalive = 90;
$cleanSession = true;
$clientId = $clientId = rand(0, 100000) . time();
$options = [
'username' => 'root',
'password' => '123456',
'keepalive' => $cleanSession,
'clean_session' => $keepalive,
'client_id' => $clientId,
'debug' => false,
'ssl' => [
'local_pk' => './mqtt_ssl/privkey.pem',
'verify_peer' => false,
],
];
$clicke = "mqtt://127.0.0.1:$port";
$mqtt = new Workerman\Mqtt\Client($clicke, $options);
$mqtt->onConnect = function ($mqtt) use ($topic, $qos) {
$mqtt->publish($topic, 'xini workerman mqtt', ['qos' => $qos]);
};
$mqtt->connect();
};
Worker::runAll();
mosquitto证书配置(这里使用的是Let's Encrypt免费证书)
![13034051-ccdf1561872442b1.png](https://i-blog.csdnimg.cn/blog_migrate/a777c932d5b83287deee0ed78e06ceca.png)
image.png