【发布时间】:2020-05-22 22:33:47
【问题描述】:
我正在节点 js 上使用 mosca 创建一个物联网服务。它不断崩溃到订阅区域,我无法显示传入的消息。
- 如何防止它崩溃。
- 如何查看收到的消息?
如何在 authorizeSubscribe 字段中查看传入消息? 还 authorizeSubscribe 字段崩溃
const mosca = require('mosca');
const settings = {
port: 1883,
};
const server = new mosca.Server(settings);
server.on('ready', setup);
function setup() {
server.authenticate = authenticate;
server.authorizePublish = authorizePublish;
server.authorizeSubscribe = authorizeSubscribe;
console.log('Mosca server is up and running');
}
const authenticate = function(client, username, password, callback) {
console.log("authenticatealanı", username + " " + password);
const authorized = (username === 'alice' && password.toString() === 'secret');
if (authorized) client.user = username;
callback(null, authorized);
};
// In this case the client authorized as alice can publish to /users/alice taking
// the username from the topic and verifing it is the same of the authorized user
const authorizePublish = function(client, topic, payload, callback) {
console.log("authorizePublish " + topic + " "+ payload);
//callback(null, client.user === topic.split('/')[1]);
};
// In this case the client authorized as alice can subscribe to /users/alice taking
// the username from the topic and verifing it is the same of the authorized user
const authorizeSubscribe = function(client, topic, message, callback) {
console.log("new Data Auth subscribe"+ topic );
console.log(message);
//callback(null, client.user === topic.split('/')[1]);
};
【问题讨论】:
-
“它崩溃”没有帮助,编辑问题以在它失败时包含错误/堆栈跟踪,以便我们了解原因。
-
没有给出错误。我不能订阅。所以我无法通过 subscribe 读取已发布的值
-
让我解释如下。一个值已发送到 topic1 / AAA。订阅了 topic1 / AAA。我使用 mqtt.fx 订阅了 topic1 / AAA。但价值观不会到来
-
所以你说的是,它不会崩溃。
-
是的,它不会崩溃,但经过验证的用户在任何主题下订阅时都没有得到响应