使用paho mqtt的javascript版本订阅主题时,无法应用通配符

时间:2018-10-22 18:28:24

标签: javascript wildcard mqtt paho

首次测试MQTT Paho javascript库,并且以下代码是文档中提供的默认示例。 当我尝试使用通配符“#”订阅主题(例如'hermes /#')时,出现此错误:

  

onConnectionLost:AMQJS0005E内部错误。错误消息:AMQJS0009E格式错误的UTF数据:80 -42。,堆栈跟踪:错误:AMQJS0009E格式错误的UTF数据:80 -42。

该文档确实非常简洁,无论如何也没有提及通配符,是js库缺少功能还是有其他方法?

    <!DOCTYPE html>
<html>
  <head>
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
  <script src="paho-mqtt.js" type="text/javascript"></script>
  <script type="text/javascript">

        var mqtt;
        var reconnectTimeout = 2000;
        var host="mywairaspi.local"; //change this
        var port= 8080;

// Create a client instance
client =  new Paho.MQTT.Client(host,port,'60');

// set callback handlers
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;

// connect the client
client.connect({onSuccess:onConnect});

// called when the client connects
function onConnect() {
  // Once a connection has been made, make a subscription and send a message.
  console.log("onConnect");
  client.subscribe("/World");
  client.subscribe('hermes/#');
  message = new Paho.MQTT.Message("Hello");
  message.destinationName = "/World";
  client.send(message); 
}

// called when the client loses its connection
function onConnectionLost(responseObject) {
  if (responseObject.errorCode !== 0) {
    console.log("onConnectionLost:"+responseObject.errorMessage);
  }
}

// called when a message arrives
function onMessageArrived(message) {
  console.log("onMessageArrived:"+message.payloadString);
}
</script>

  </head>
  <body>
  </body>

</html>

1 个答案:

答案 0 :(得分:0)

我相信您会在PAHO客户端中看到一个错误,因为通配符当然受到支持。截至2018年11月,如果接收到的任何消息都是原始二进制数据(或根本没有解析为有效的UTF),那么它将给出``格式错误的UTF数据''错误。

已在github上添加了一个pull-request,为我修复了它,希望它将很快合并到一个发行版中: https://github.com/eclipse/paho.mqtt.javascript/pull/178