配置 Mosquitto 服务器以接收来自 javascript 的消息
Configuration of Mosquitto Server to receive message from javascript
我有一个关于 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));
...
我有一个关于 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));
...