v1.6 2024-06-04 14:22 UTC

This package is auto-updated.

Last update: 2024-09-04 14:52:13 UTC


README

基于 workerman 的异步 MQTT 客户端,适用于 PHP。

安装

composer require workerman/mqtt

文档

中文文档

示例

subscribe.php

<?php
require __DIR__ . '/vendor/autoload.php';
use Workerman\Worker;
$worker = new Worker();
$worker->onWorkerStart = function(){
    $mqtt = new Workerman\Mqtt\Client('mqtt://test.mosquitto.org:1883');
    $mqtt->onConnect = function($mqtt) {
        $mqtt->subscribe('test');
    };
    $mqtt->onMessage = function($topic, $content){
        var_dump($topic, $content);
    };
    $mqtt->connect();
};
Worker::runAll();

使用命令 php subscribe.php start 运行

publish.php

<?php
require __DIR__ . '/vendor/autoload.php';
use Workerman\Worker;
$worker = new Worker();
$worker->onWorkerStart = function(){
    $mqtt = new Workerman\Mqtt\Client('mqtt://test.mosquitto.org:1883');
    $mqtt->onConnect = function($mqtt) {
       $mqtt->publish('test', 'hello workerman mqtt');
    };
    $mqtt->connect();
};
Worker::runAll();

使用命令 php publish.php start 运行

API

__construct (string $address, [array $options])

通过 $address 和 $options 创建实例。

  • $address 可以是以下协议之一: 'mqtt', 'mqtts', 'mqtt://test.mosquitto.org:1883'。

  • $options 是客户端连接选项。默认值

    • keepalive: 50 秒,设置为 0 以禁用
    • client_id: 客户端 ID,默认 workerman-mqtt-client-{$mt_rand}
    • protocol_name: 'MQTT' 或 'MQIsdp'
    • protocol_level: 'MQTT'4 和 'MQIsdp' 是 3
    • clean_session: true,设置为 false 以接收离线时的 QoS 1 和 2 消息
    • reconnect_period: 1 秒,两次重连之间的间隔
    • connect_timeout: 30 秒,等待 CONNACK 的超时时间
    • username: 代理所需的用户名,如有需要
    • password: 代理所需的密码,如有需要
    • will: 当客户端意外断开连接时,代理会自动发送的消息。格式是
      • topic: 发布的主题
      • content: 要发布的消息
      • qos: QoS
      • retain: 保留标志
    • resubscribe : 如果连接中断并重新连接,订阅的主题将自动重新订阅(默认 true
    • bindto 默认 '', 用于指定 PHP 将用于访问网络的 IP 地址
    • ssl 默认 false,可以设置为 truessl context,请参阅 https://php.ac.cn/manual/en/context.ssl.php
    • debug 默认 false,设置为 true 以显示调试信息

connect()

连接到由 __construct($address, $options) 中指定的 $address 和 $options 指定的代理。

publish(String $topic, String $content, [array $options], [callable $callback])

向主题发布消息

  • $topic 是要发布的主题,String
  • $message 是要发布的消息,String
  • $options 是发布选项,包括
    • qos QoS 级别,Number,默认 0
    • retain 保留标志,Boolean,默认 false
    • dup 标记为重复标志,Boolean,默认 false
  • $callback - function (\Exception $exception),当 QoS 处理完成时触发,或者在 QoS 0 的下一个 tick 时触发。如果没有错误发生,则 $exception 将为 null。

subscribe(mixed $topic, [array $options], [callable $callback])

订阅一个或多个主题

  • $topic 是一个 String 主题或一个包含主题名称作为键和 QoS 作为值的 Array,例如 array('test1'=> 0, 'test2'=> 1),用于订阅。
  • $options 是订阅选项,包括
    • qos QoS 订阅级别,默认 0
  • $callback - function (\Exception $exception, array $granted) 当收到 suback 时触发的回调,其中
    • exception 是订阅错误或在客户端断开连接时发生的错误
    • granted 是一个数组,例如 array('topic' => 'qos', 'topic' => 'qos'),其中
      • topic 是已订阅的主题
      • qos 是授予的主题 QoS 级别

unsubscribe(mixed $topic, [callable $callback])

从主题或主题中取消订阅

  • $topic 是一个 String 主题或一个要取消订阅的主题数组
  • $callback - function (\Exception $e),在 unsuback 时触发。如果没有错误发生,则 $exception 将为 null。

disconnect()

向代理发送 DISCONNECT 数据包并关闭客户端。

close()

关闭客户端而不发送 DISCONNECT 数据包。

callback onConnect(Client $mqtt)

在成功连接时触发(接收到 CONNACK 数据包)。

callback onMessage(String $topic, String $content, Client $mqtt)

function (topic, message, packet) {}

当客户端收到发布数据包时触发

  • $topic 接收到的数据包的主题
  • $content 接收到的数据包的有效负载
  • $mqtt 客户端实例。

callback onError(\Exception $exception)

当发生错误时触发,例如客户端无法连接到代理。

callback onClose()

在连接关闭时触发。

许可协议

MIT