简体   繁体   English

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

[英]Can't apply wildcard when subscribe to a topic with the javascript version of paho mqtt

Testing the MQTT Paho javascript library for the first time and the following code is the default example present on the documentation. 首次测试MQTT Paho javascript库,并且以下代码是文档中提供的默认示例。 As soon as I try to use a wildcard "#" for subscribing to a topic (for example 'hermes/#' ) I get this error: 一旦我尝试使用通配符“#”订阅主题(例如'hermes /#'),就会出现此错误:

onConnectionLost:AMQJS0005E Internal error. onConnectionLost:AMQJS0005E内部错误。 Error Message: AMQJS0009E Malformed UTF data:80 -42 ., Stack trace: Error: AMQJS0009E Malformed UTF data:80 -42 . 错误消息:AMQJS0009E格式错误的UTF数据:80 -42。,堆栈跟踪:错误:AMQJS0009E格式错误的UTF数据:80 -42。

The documentation is really terse and anyway doesn't mention anything about wildcards, is that a missing feature on the js library or there is a different way? 该文档确实非常简洁,无论如何也没有提及通配符,是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>

I believe you're seeing a bug in the PAHO client, as wildcards are certainly supported. 我相信您会在PAHO客户端中看到一个错误,因为肯定支持通配符。 As of Nov 2018, if any of the messages received are raw binary data (or simply not parsed as valid UTF), then it gives that "Malformed UTF data" error. 截至2018年11月,如果接收到的任何消息都是原始二进制数据(或根本没有解析为有效的UTF),那么它将给出``格式错误的UTF数据''错误。

A pull-request on github has been added that fixed it for me, and hopefully it will get merged in to a release soon: https://github.com/eclipse/paho.mqtt.javascript/pull/178 已在github上添加了pull-request,已为我修复了该请求,并希望它将很快合并到一个发行版中: https : //github.com/eclipse/paho.mqtt.javascript/pull/178

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM