无法使用 Javascript Ionic 连接到 MQTT 代理(mosquitto)
Cannot connect to MQTT broker(mosquitto) with Javascript Ionic
我尝试将 MQTT 与 Ionic 框架绑定。当尝试向代理发送 MQTT 消息时(我正在发布),当点击事件发生时,我收到以下错误:
"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
函数会在return连接完成前
所以在连接完成之前正在调用 send
。
connect
函数可以接受一个参数,该参数可以包含一个回调函数,以便在连接完成后调用。所以像这样的东西应该可以工作
client.connect({onSuccess: function(){
message = new Paho.MQTT.Message("Hello");
message.destinationName = "test/smit";
client.send(message);
}});
我尝试将 MQTT 与 Ionic 框架绑定。当尝试向代理发送 MQTT 消息时(我正在发布),当点击事件发生时,我收到以下错误:
"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
函数会在return连接完成前
所以在连接完成之前正在调用 send
。
connect
函数可以接受一个参数,该参数可以包含一个回调函数,以便在连接完成后调用。所以像这样的东西应该可以工作
client.connect({onSuccess: function(){
message = new Paho.MQTT.Message("Hello");
message.destinationName = "test/smit";
client.send(message);
}});