我已经下载了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'对于在线经纪人来说,它有效。所以,我猜测我的问题是在我的经纪人中允许端口。
有谁知道如何解决这个问题?