【问题标题】:Can't apply wildcard when subscribe to a topic with the javascript version of paho mqtt使用 paho mqtt 的 javascript 版本订阅主题时无法应用通配符
【发布时间】:2018-10-22 18:28:24
【问题描述】:

首次测试 MQTT Paho javascript 库,以下代码是文档中的默认示例。 一旦我尝试使用通配符“#”订阅主题(例如 'hermes/#' ),我就会收到此错误:

onConnectionLost:AMQJS0005E 内部错误。错误消息:AMQJS0009E Malformed UTF data:80 -42 ., Stack trace: Error: AMQJS0009E Malformed UTF data: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>

【问题讨论】:

  • 您发布的代码没有任何问题。编辑问题以在您运行它时将任何输出包含到浏览器控制台(并在事物之间添加更多console.log()。此外,您从哪里得到paho-mqtt.js 文件,预建的都是mqttws31.jsiirc
  • 我已经从 eclipse 网站 link 下载了库 ( paho.javascript-1.0.3.zip ) 我从控制台得到的唯一输出是我在上面发布的错误,我已经试图打印出 message 对象,但似乎使用通配符后代码停止
  • subscribe()client.send() 之后添加console.log。尝试使用 mosquitto_sub 之类的东西来检查当时正在发送的消息。

标签: javascript wildcard mqtt paho


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多