gpt4 book ai didi

node.js - MQTT 嵌入 mosca,无需任何代理

转载 作者:太空宇宙 更新时间:2023-11-04 03:06:17 25 4
gpt4 key购买 nike

我想以嵌入式方式使用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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com