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



首次测试 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>

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

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

最新更新