我对Mosquitto和MQTT有一个一般性的问题。我设法通过Websockets(tutorial here)设置了运行Ubuntu和Mosquitto 1.4.2的服务器。我想使用mqttws31.js从Javascript应用程序发送一些数据。订阅test.mosquitto.org时,两者都工作正常,但是当我尝试将“ Hello world”发送到我自己的服务器时,出现超时。
我尝试过的是:
ubuntu@instanz6:~$ mosquitto -c /etc/mosquitto/mosquitto.conf
1440670543: mosquitto version 1.4.2 (build date 2015-08-27 11:26:53+0200) starting
1440670543: Config loaded from /etc/mosquitto/mosquitto.conf.
1440670543: Opening websockets listen socket on port 9001.
1440670543: Opening ipv4 listen socket on port 1883.
1440670543: Opening ipv6 listen socket on port 1883.
,然后打开如下所示的html文件。我想我犯了一个普遍错误/错过了一个概念,但我不知道如何继续...
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<script src="js/mqttws31.js" type="text/javascript"></script>
<script type="text/javascript">
var wsbroker = "11.111.11.111";
var wsport = 1883;
var client = new Paho.MQTT.Client(wsbroker, wsport,
"myclientid_" + parseInt(Math.random() * 100, 10));
client.onConnectionLost = function (responseObject) {
console.log("connection lost: " + responseObject.errorMessage);
};
client.onMessageArrived = function (message) {
console.log(message.destinationName, ' -- ', message.payloadString);
};
var options = {
timeout: 3,
onSuccess: function () {
console.log("mqtt connected");
// Connection succeeded; subscribe to our topic, you can add multile lines of these
//client.subscribe('/World', {qos: 1});
//use the below if you want to publish to a topic on connect
message = new Paho.MQTT.Message("Hello");
message.destinationName = "/World";
client.send(message);
},
onFailure: function (message) {
console.log("Connection failed: " + message.errorMessage);
}
};
function init() {
client.connect(options);
}
</script>
</head>
<body onload="init();">
</body>
</html>
您的JavaScript中的端口错误,它必须是为Websockets配置的端口9001,而不是1883
...
var wsbroker = "11.111.11.111";
var wsport = 1883;
var client = new Paho.MQTT.Client(wsbroker, wsport,
"myclientid_" + parseInt(Math.random() * 100, 10));
...
应该是
...
var wsbroker = "11.111.11.111";
var wsport = 9001;
var client = new Paho.MQTT.Client(wsbroker, wsport,
"myclientid_" + parseInt(Math.random() * 100, 10));
...
我什至遇到相同的错误,连接失败:AMQJSC0001E连接超时。
端口是开放的,通过移动应用程序,一切正常
仅通过网络应用,这是连接失败