Received subscribe acknowledgement with invalid QoS values from the broker

服务端:emqx

客户端:php-mqtt/client 3.x

PHP≥7.4

  • 需求

    • 项目需要自定义订阅客户端的离线和上线
  • 代码

    $server   = 'emqx server url';
    $port     = 1883;
    $clientId = 'sys_client';
    $username = 'test';
    $password = '12345678';
    $topics='$SYS/brokers/+/clients/#';//订阅客户端上线下线
    $mqtt = new \PhpMqtt\Client\MqttClient($server, $port, $clientId);
    $connectionSettings = (new \PhpMqtt\Client\ConnectionSettings)
        ->setUsername($username)
        ->setPassword($password);
    
    $mqtt->connect($connectionSettings, true);
    $mqtt->subscribe($topics, function ($topic, $message) {
    			echo sprintf("Received message on topic [%s]: %s\n", $topic, $message);
    		}, 0);
    $mqtt->loop(true);
    $mqtt->disconnect();
    
  • 为什么会出现异常?

    Received subscribe acknowledgement with invalid QoS values from the broker.
    
  • 如何解决?

    • 参考EMQX的ACL文档做规则设置:https://www.emqx.io/docs/zh/v4.3/advanced/acl-file.html

    • 默认的acl.conf

      {allow, {user, "dashboard"}, subscribe, ["$SYS/#"]}.
       
      {allow, {ipaddr, "127.0.0.1"}, pubsub, ["$SYS/#", "#"]}.
       
      {deny, all, subscribe, ["$SYS/#", {eq, "#"}]}.
       
      {allow, all}.
      
    • 修改acl.conf,为了安全起见,我这里做了只允许指定的ip订阅$SYS/brokers/+/clients/#

      {allow, {user, "dashboard"}, subscribe, ["$SYS/#"]}.
       
      {allow, {ipaddr, "127.0.0.1"}, pubsub, ["$SYS/#", "#"]}.
      
      #指定ip订阅
      {allow, {ipaddr, "127.0.0.1"}, subscribe, ["$SYS/brokers/+/clients/#"]}.
      #允许所有客户端订阅
      #{allow, all, subscribe, ["$SYS/brokers/+/clients/#"]}.
      
      {deny, all, subscribe, ["$SYS/#", {eq, "#"}]}.
       
      {allow, all}.
      
    • 然后,通过命令行的方式运行一下,应该就不会报错了

posted @ 2022-02-25 10:49  深海空气  阅读(124)  评论(0编辑  收藏  举报