首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PHPMQTT v1.3.2 版本发布,优化 MQTT5 相关支持

PHPMQTT v1.3.2 版本发布,优化 MQTT5 相关支持

作者头像
沈唁
发布2021-03-11 10:46:32
3580
发布2021-03-11 10:46:32
举报
文章被收录于专栏:沈唁志沈唁志
  1. 添加 toArray 方法

在上个版本中为 getContents 方法增加了一个 getArray 参数来用于客户端回复对端 ACK,此版本中增加了 toArray 方法进行获取:

use Simps\MQTT\Message;
use Simps\MQTT\Protocol\ProtocolInterface;

$message = new Message\Publish();
$message->setProtocolLevel(ProtocolInterface::MQTT_PROTOCOL_LEVEL_5_0)
    ->setTopic('simps/mqtt/message')
    ->setQos(ProtocolInterface::MQTT_QOS_1)
    ->setDup(ProtocolInterface::MQTT_DUP_0)
    ->setRetain(ProtocolInterface::MQTT_RETAIN_0)
    ->setMessage('this is content')
    ->setMessageId(1)
    ->setProperties(['message_expiry_interval' => 100]);

$array1 = $message->getContents(true);
$array2 = $message->toArray();
assert($array1 === $array2);

两者的结果相同。

  1. 优化 getProtocolLevel

MQTT5 协议中增加了一个 Properties 属性,而 MQTT3.x 中是没有的,之前的版本如果是 MQTT5 协议的话需要手动调用setProtocolLevel来设置协议等级,此版本中就增加优化:判断是否设置了Properties 属性,如果设置了但协议等级不是 MQTT5 则自动设置为 MQTT5

  • Message
use Simps\MQTT\Message;
use Simps\MQTT\Protocol\ProtocolInterface;

$message = new Message\Publish();
$message->setTopic('simps/mqtt/message')
    ->setQos(ProtocolInterface::MQTT_QOS_1)
    ->setDup(ProtocolInterface::MQTT_DUP_0)
    ->setRetain(ProtocolInterface::MQTT_RETAIN_0)
    ->setMessage('this is content')
    ->setMessageId(1);

assert($message->isMQTT5() === false);
assert($message->getProtocolLevel() === ProtocolInterface::MQTT_PROTOCOL_LEVEL_3_1_1);

$message->setProperties(['message_expiry_interval' => 100]);
assert($message->isMQTT5() === true);
assert($message->getProtocolLevel() === ProtocolInterface::MQTT_PROTOCOL_LEVEL_5_0);

$message->setProtocolLevel(ProtocolInterface::MQTT_PROTOCOL_LEVEL_3_1);
assert($message->isMQTT5() === true);
assert($message->getProtocolLevel() === ProtocolInterface::MQTT_PROTOCOL_LEVEL_5_0);
  • Config
use Simps\MQTT\Client;
use Simps\MQTT\Config\ClientConfig;
use Simps\MQTT\Protocol\ProtocolInterface;

$config = new ClientConfig();
$config->setClientId(Client::genClientID())
    ->setKeepAlive(10)
    ->setDelay(3000)
    ->setMaxAttempts(5)
    ->setProperties([
        'session_expiry_interval' => 100,
    ])
    ->setSwooleConfig([
        'open_mqtt_protocol' => true,
        'package_max_length' => 2 * 1024 * 1024,
    ]);

assert($config->isMQTT5() === true);
assert($config->getProtocolLevel() === ProtocolInterface::MQTT_PROTOCOL_LEVEL_5_0);

更新日志

增强

  • 添加 toArray 方法 (b3fd28a)
  • 更新属性默认值 (9c63510)
  • 添加遗嘱消息 Message 类 (#45)
  • 添加 Auth Message 类 (36f6a9d)
  • 增加 DUP、SESSION_PRESENT 和 RETAIN 的常量 (fe5c418)
  • 优化 getProtocolLevel (a329202)
  • 增加 isMQTT5 测试和使用常量代替硬编码(b9d4365)

修复

  • 修正 ReasonCode 中错字 (481994f5)

关于 PHPMQTT

  • 适用于 PHP 的 MQTT 协议解析和协程客户端
  • 支持 MQTT 协议 3.1、3.1.1 和 5.0 版本,支持 QoS 0、QoS 1、QoS 2
  • 首个支持 MQTT v5.0 协议的 PHP library

文档:https://mqtt.simps.io GitHub:https://github.com/simps/mqtt Gitee:https://gitee.com/phpiot/mqtt

支持记得点个 Star~

好文和朋友一起看~

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-03-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 沈唁志 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 更新日志
    • 增强
      • 修复
      • 关于 PHPMQTT
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档