猫言猫语

严以律己·宽以待人·自强不息·知行合一

使用 php-mqtt/client 进行 MQTT 消息发布与订阅

| 使用 php-mqtt/client 进行 MQTT 消息发布与订阅已关闭评论

composer require php-mqtt/client
<?php
require __DIR__.'/vendor/autoload.php';

use PhpMqtt\Client\MqttClient;

$client = new MqttClient('127.0.0.1', 1883, 'client1');
$client->connect();
$client->publish('topic', 'Hello World!');
$client->disconnect();
<?php
require __DIR__.'/vendor/autoload.php';

use PhpMqtt\Client\MqttClient;

$client = new MqttClient('127.0.0.1', 1883, 'client2');
$client->connect();
$client->subscribe('topic', function($topic, $message) {
    echo $topic, $message, PHP_EOL;
});
$client->loop(true);
$client->disconnect();

评论已关闭。