workman使用mqtt

发布消息

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免费证书)


image.png

你可能感兴趣的:(workman使用mqtt)