# MqttClient 异步MQTT客户端,可以结合EMQ开源服务器实现百万千万级通讯业务。 支持完整的MQTT协议规则 纯异步,断线重连 实例代码: ```php /** * 这里可以进行额外的异步连接池,比如另一组redis/mysql连接 * @param $workerId * @return array */ public function initAsynPools($workerId) { parent::initAsynPools(); if($workerId==0) { $mqtt = new MQTT('tcp://127.0.0.1:11883/','root1'); //设置持久会话 $mqtt->setConnectClean(false); //认证 $mqtt->setAuth('root1','root'); //存活时间 $mqtt->setKeepalive(3600); //回调 $mqtt->on('publish', function ($mqtt, PUBLISH $publish_object) { printf( "\e[32mI got a message\e[0m:(msgid=%d, QoS=%d, dup=%d, topic=%s) \e[32m%s\e[0m\n", $publish_object->getMsgID(), $publish_object->getQos(), $publish_object->getDup(), $publish_object->getTopic(), $publish_object->getMessage() ); }); $mqtt->on('connack', function (MQTT $mqtt, CONNACK $connack_object) { var_dump("MQTT连接成功"); $topics['$SYS/#'] = 1; $mqtt->subscribe($topics); }); $mqtt->connect(); } } ```