1

我是物联网世界的新手,我正在尝试通过 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>节点服务器.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请解释和downvote它。

4

0 回答 0