0

我有一个远程设备,它只是一个传感器。它会不断地给我一些数字。实际上,我在我的node.js服务器中收到了这些数字,我不想发布这些数据。我想订阅我的客户,并想在客户订阅后打印我的主题消息。这是我的Node.js服务器和c中的 mqtt 。在./a.out之后使用node app.jsgcc main.c运行这两个文件。 我该如何解决这个问题? 文件如下所示:

应用程序.js

'use strict';

//dependencies
var express          = require('express'),
    passport         = require('passport'),
    strategy         = require('passport-local').Strategy,
    mongoose         = require('mongoose'),
    mongodb          = require('mongodb'),
    bodyParser       = require('body-parser'),
    path             = require('path'),
    acl              = require('acl'),
    mosca            = require('mosca');   

exports.init = function(pp) {
    passport = pp;
    app = pp;
    return exports;
};

exports.root = function(req, res) {
    // Running
    res.send("Running");
};

//create express app
var app         = express();
    app.appname ="watersensor_DB";

//config mongoose
  mongoose.Promise = global.Promise;
  app.db = mongoose.createConnection('localhost/'+app.appname);
  app.db.on('error', console.error.bind(console, 'mongoose connection error: '));
  app.db.once('open', function () {
    // Store All Data
  });

//config mongodb
  mongodb.connect("mongodb://localhost/"+app.appname, function(error, mdb) {
  app.acl=new acl(new acl.mongodbBackend(mdb, app.appname));
  });

//config data models
  require('./models')(app, mongoose);

//Serve Frontend
  app.use(express.static(path.join(__dirname, '/public/')));

//config Routes
  var router = express.Router();
  require('./routes')(app, router, passport);

//config express
  app.set('secret','thisshouldnotbeinplaintext');
  app.use(bodyParser.urlencoded({ extended: false }));
  app.use(bodyParser.json());
  app.use(passport.initialize());
  app.use(router);


//config mosca
var ascoltatore = {
  //using ascoltatore 
  type: 'mongo',
  url: 'mongodb://localhost:27017/mqtt',
  pubsubCollection: 'ascoltatori',
  mongo: {}
};

var settings = {
  port: 1883,
  backend: ascoltatore,
  persistence: {
    factory: mosca.persistence.Mongo,
    url: 'mongodb://localhost:27017/mqtt'
  }
};

var server = new mosca.Server(settings);

server.on('clientConnected', function(client) {
    console.log('client connected', client.id);
});

// fired when a message is received 
server.on('published', function(packet, client) {
  console.log('Published', packet.payload); 
});


server.on('ready', setup);

// fired when the mqtt server is ready 
function setup() {
  console.log('Mosca server is up and running');
}

//Port Listening
  app.listen(7000, function(){
    //Running
    console.log("Node.js Server Is Running On localhost:7000");
  });

主程序

#include <stdio.h>

main()
{
    char buf[1024];
    int i;  
    //mosquitto_sub -t 'test/topic' -v
    //mosqui<to_pub -t 'test/topic' -m 'hello'
    for(i=0; ;i++) {
        //sprintf(buf, "mosquitto_pub -h 192.168.43.82 -p 1883 -t 'test' -m '%d'",i);
        sprintf(buf, "mosquitto_pub -t 'test' -m '%d'",i);
        printf("%s\n",buf);
        system(buf);
        sleep(1);
    }
}
4

1 回答 1

-1

您的意思是 node.js 服务器获取从您的远程设备发布的数据?之后,服务器将数据存储在MongoDB中???

对于这个意思,服务器不需要订阅客户端。它可以从下面的代码中获取数据。

server.on('published', function(packet, client) {
    console.log('Published : ', packet.topic + " --- " + packet.payload);

    var MongoClient = require('mongodb').MongoClient;
    var url = "mongodb://localhost:27017/mydb";

    MongoClient.connect(url, function(err, db) {
      if (err) throw err;

      var stringBuf = packet.payload.toString('utf-8');
      var myobj3 = JSON.parse(stringBuf);

      db.collection("customers").insertOne(myobj3, function(err, res) {
        if (err) throw err;
        console.log("1 record inserted");
        db.close();
      });
    });
});
于 2017-05-30T06:47:52.080 回答