我是IoT世界的新手, 我试图通过mqtt协议连接服务器,在FE中使用paho。 后端在节点和mosca中作为经纪人。 当我尝试从angularjs app听顶部时,得到以下信息。
WebSocket connection to 'ws://127.0.0.1:1996/mqtt' failed: WebSocket opening handshake timed out
并在mqttClient.connect - > onFailure中,获取此错误obj
{
errorCode:1,
errorMessage:"AMQJSC0001E Connect timed out.",
invocationContext:undefined
}
c:\ nodejsPrjct> node server.js
server.js内容
// set up =========
var express = require('express');
var app = express();
/*
* log requests to the console (express4)
* */
var morgan = require('morgan') || '';
/*
* listing port for application
* */
var port = 1956 || process.env.PORT || 8080;
/*
* pull information from HTML POST
* */
var bodyParser = require('body-parser');
//config =========
/*
* SPA client folder
* */
app.use(express.static('client'));
/*
* comments for each request on console
* */
app.use(morgan('dev'));
/*
* limits json files to 50mb
* **/
app.use(bodyParser.json({limit: '50mb'}));
// routes =========
require('./routes/routes')(app);
/*
* MongoDb listener
* */
var ascoltatori = require('ascoltatori');
var pubsubsettings = {
type: 'mongo',
url: 'mongodb://localhost:27017/mqtt',
pubsubCollection: 'ascoltatori',
mongo: {}
};
var settings = {
port: 1996,
backend: pubsubsettings
};
/*
* Message broker
* */
var mosca = require('mosca');
var server = new mosca.Server(settings, function () {
//callback if server is slow
});
var message = {
topic: 'cross',
payload: parseInt(Math.random() * 100, 10), // or a Buffer
qos: 1, // 0, 1, or 2
retain: true // or false
};
// fired when the mqtt server is ready
server.on('ready', serverReady);
// fired when a message is received
server.on('published', serverPublished);
// fired when client connected and ready
server.on('clientConnected', clientConnectedSuccesful);
function serverReady(data) {
console.log('Mosca server is up and running', data);
}
function serverPublished(packet, client) {
console.log('Published', packet.payload);
console.log('Published', client);
}
function clientConnectedSuccesful(client) {
console.log('client connected', client.id);
setInterval(function () {
server.publish(message, function () {
message.payload = parseInt(Math.random() * 100, 10);
});
}, 15000);
}
// init =========
app.listen(port);
来自angular.js的>
/*
* https://www.eclipse.org/paho/files/jsdoc/symbols/Paho.MQTT.Client.html
* */
var mqttClient = undefined;
var location = {
'hostname': '127.0.0.1',
'port': 1996
};
var mqttClientId = "myclientid_" + parseInt(Math.random() * 100, 10);
// Create a client instance
//Paho.MQTT.Client(host, port, path, clientId)
mqttClient = new Paho.MQTT.Client(location.hostname, location.port, mqttClientId);
mqttClient.onMessageArrived = function (message) {
console.log("onMessageArrived:" + message.payloadString);
$scope.currentValues = message.payloadString;
};
mqttClient.onConnectionLost = function (responseObject) {
if (responseObject.errorCode !== 0) {
console.log("onConnectionLost:" + responseObject.errorMessage);
}
};
var options = {
timeout: 5,
onSuccess: function (data) {
mqttClient.subscribe("cross", {qos: 1});
console.log("onConnect", data);
}, onFailure: function (data) {
console.log(data)
}
};
mqttClient.connect(options);
* downvoter请解释并投票。