关于php:PHPMQTT-v131-版本发布MQTT-协议解析-协程客户端

2次阅读

共计 1619 个字符,预计需要花费 5 分钟才能阅读完成。

上个版本公布的知乎文章中有用户评论,遇到了握手失败的状况,分割了我之后进行了测试,我这里的确没问题,不过我删除了一段代码:

$will = ['topic' => '','qos'=> 0,'retain'=> 0,'message'=>'',];
$client->connect(true, $will);

改为了间接进行 connect

$client->connect();

因为应用的是 MQTT3,所以没有 MQTT5 中的 code 属性,须要通过抓包才能够获取到谬误起因。

这里换为 MQTT5 之后就能够间接获取到谬误起因,如以下代码就能够获取到谬误起因

use Simps\MQTT\Client;
use Simps\MQTT\Config\ClientConfig;
use Simps\MQTT\Hex\ReasonCode;
use function Swoole\Coroutine\run;

run(function () {$config = new ClientConfig();
    $config->setUserName('')
        ->setPassword('')
        ->setClientId(Client::genClientID())
        ->setKeepAlive(10)
        ->setProtocolLevel(5)
        ->setDelay(3000) // 3s
        ->setMaxAttempts(5)
        ->setSwooleConfig([
            'open_mqtt_protocol' => true,
            'package_max_length' => 2 * 1024 * 1024,
        ]);

    $client = new Client('broker.emqx.io', 1883, $config);
    $will = ['topic' => '','qos'=> 0,'retain'=> 0,'message'=>'byebye',];
    $res = $client->connect(true, $will);
    var_dump($res, ReasonCode::getReasonPhrase($res['code']));
});

将 code 换为起因,就能够获取到谬误起因为:Topic Name invalid

array(3) {["type"]=>
  int(2)
  ["session_present"]=>
  int(0)
  ["code"]=>
  int(144)
}
string(18) "Topic Name invalid"

查看 MQTT 协定文档,会发现:

The Will Topic MUST be a UTF-8 Encoded String as defined

遗嘱音讯中的 topic 不能为空,必须是一个无效的 UTF-8 字符串,长度大于 0

所以这里此版本中减少了限度,如果存在遗嘱音讯,并且 topic 为空,则会间接抛出异样:

PHP Fatal error:  Uncaught Simps\MQTT\Exception\ProtocolException: Topic cannot be empty

更新日志

加强

  • 减少 isMQTT5 办法 (f27f85c)
  • 减少常量可见性 (#41)
  • 为 getContents 办法增加 getArray 参数来用于客户端回复对端 ACK (#42)
  • 增加 Message 测试 (8f7fe30)
  • 更新 Message 文档 (a41654b)
  • 更新 ProtocolException 并减少测试 (19a6bee)

修复

  • 修复遗嘱音讯的 topic 不能为空 (b56fda1)

对于 PHPMQTT

  • MQTT 协定解析 & 协程客户端
  • 实用于 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~

正文完
 0