【发布时间】: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