我想以嵌入式方式使用node.js mosca MQTT代理。

从文档中看,嵌入式mosca似乎必须使用外部代理,例如Mosquitto,AMQP,ZeroMQ,Redis。对于独立的mosca,不需要外部代理。可以运行嵌入式mosca以便不使用类似于独立mosca的外部代理吗?

如果可以做到,那么下面使用redis的代码应该如何修改?

var mosca = require('mosca')

var ascoltatore = {
  type: 'redis',
  redis: require('redis'),
  db: 12,
  port: 6379,
  return_buffers: true, // to handle binary payloads
  host: "localhost"
};

var moscaSettings = {
  port: 1883,
  backend: ascoltatore,
  persistence: {
    factory: mosca.persistence.Redis
  }
};

var server = new mosca.Server(moscaSettings);
server.on('ready', setup);

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);
});

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

最佳答案

这是使用以嵌入式方式运行的mosca的安全MQTT代理的工作示例的完整源代码。

var mosca = require('mosca')

var SECURE_KEY = __dirname + '/key.pem';
var SECURE_CERT = __dirname + '/cert.pem';

var settings = {
    port: 1883,
    logger: {
        name: "secureSample",
        level: 40,
    },
    secure : {
        port: 7998,
        keyPath: SECURE_KEY,
        certPath: SECURE_CERT,
    }
};
var server = new mosca.Server(settings);
server.on('ready', setup);

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);
});

// fired when the mqtt broker is ready
function setup() {
    console.log('Mosca embedded MQTT broker running now')
}

关于node.js - 没有任何代理的MQTT嵌入式mosca,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39989111/

10-11 06:56