首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用nodejs mosca的身份验证和公开订阅

使用nodejs mosca的身份验证和公开订阅
EN

Stack Overflow用户
提问于 2020-05-22 22:33:47
回答 1查看 286关注 0票数 0

我正在为节点js上的mosca创建一个物联网服务。它经常崩溃到订阅区域,我无法显示传入的消息。

  1. 如何防止它崩溃。
  2. 如何看到传入的消息?

如何在authorizeSubscribe字段中看到传入的消息?也是authorizeSubscribe字段崩溃

代码语言:javascript
运行
复制
 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]);
 };
EN

回答 1

Stack Overflow用户

发布于 2020-05-24 17:43:42

如果您的授权函数从未调用在callback中传递的内容,那么它们将永远不会授权任何内容.

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61964879

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档