我已经下载了 Mosca (^1.1.2)、MQTT(通过 npm)和 Paho。当我创建一个简单的代理时,如下所示:http: //thejackalofjavascript.com/getting-started-mqtt/(最后 3 个代码)。一切正常。我的问题是当我尝试使用 Paho 在浏览器中实现客户端时。使用此代码:
// Create a client instance
var client = new Paho.MQTT.Client('127.0.0.1', 4883, "clientId-1");
// set callback handlers
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
var options = {
//connection attempt timeout in seconds
timeout: 3,
//Gets Called if the connection has successfully been established
onSuccess: function () {
console.log("onConnect");
client.subscribe("testtopic/#");
},
//Gets Called if the connection could not be established
onFailure: function (message) {
console.log("Connection failed: " + message.errorMessage);
}
};
// connect the client
client.connect(options);
// called when the client connects
function onConnect() {
console.log("onConnect");
client.subscribe("testtopic/#");
}
// 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(message.payload);
}
我总是收到此消息:“连接失败:AMQJSC0001E 连接超时。”
当我将“127.0.0.1”更改为在线经纪人时,它可以工作。所以,我猜我的问题是在我的代理中允许端口。
有谁知道如何解决这个问题?