无法使用Javascript Ionic连接到MQTT代理(mosquitto)
问题描述:
我尝试将MQTT与Ionic框架绑定。当试图发送一个MQTT消息给代理(我出版),当点击事件发生时,我得到以下错误:无法使用Javascript Ionic连接到MQTT代理(mosquitto)
"AMQJS0011E Invalid state not connected......" "WebSocket connection to 'ws://test.mosquitto.org:8080/mqtt' failed: Error >during WebSocket handshake: net::ERR_CONNECTION_RESET"
请谁能帮助我?我无法在任何地方找到解决方案。
我也尝试过使用端口1883,但问题依然存在。
var App = angular.module("App",["ionic","ngWebSocket"]);
App.controller("Appctrl",["$scope","$log",Appctrl]);
function Appctrl($scope,$log,$websocket){
$scope.mqtt_on = function() {
client = new Paho.MQTT.Client("test.mosquitto.org",
Number(8080),"zsrgdxrgdt");
client.connect();
message = new Paho.MQTT.Message("Hello");
message.destinationName = "test/smit";
client.send(message);
alert("ON");
};
$scope.mqtt_off = function() {
alert("Off");
`enter code here`};
};
答
的Paho Javascript客户端是异步的,connect
函数将返回之前完成连接
所以正在取得调用send
之前完成连接。
connect
函数可以接受一个参数,该参数可以包含一个连接完成后调用的回调函数。所以像这样的东西应该工作
client.connect({onSuccess: function(){
message = new Paho.MQTT.Message("Hello");
message.destinationName = "test/smit";
client.send(message);
}});
谢谢你兄弟的作品 –