简体   繁体   中英

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. As soon as I try to use a wildcard "#" for subscribing to a topic (for example 'hermes/#' ) I get this error:

onConnectionLost:AMQJS0005E Internal error. Error Message: AMQJS0009E Malformed UTF data:80 -42 ., Stack trace: Error: AMQJS0009E Malformed UTF data: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?

    <!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. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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