workerman / mqtt
v1.6
2024-06-04 14:22 UTC
Requires
- workerman/workerman: >=3.3.0
Suggests
- ext-event: For better performance.
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
Client::__construct()
Client::connect()
Client::publish()
Client::subscribe()
Client::unsubscribe()
Client::disconnect()
Client::close()
回调 onConnect
回调 onMessage
回调 onError
回调 onClose
__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
: QoSretain
: 保留标志
resubscribe
: 如果连接中断并重新连接,订阅的主题将自动重新订阅(默认true
)bindto
默认 '', 用于指定 PHP 将用于访问网络的 IP 地址ssl
默认false
,可以设置为true
或ssl context
,请参阅 https://php.ac.cn/manual/en/context.ssl.phpdebug
默认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