使用Node.js Mosca进行身份验证和发布订阅

我正在使用mosca在节点js上创建物联网服务。它不断崩溃到订阅区域,我无法显示收到的消息。

  1. 如何防止其崩溃。
  2. 如何查看收到的消息?

如何在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,message,callback) {
     console.log("new Data Auth subscribe"+ topic );

     console.log(message);

     //callback(null,client.user === topic.split('/')[1]);
 };
iCMS 回答:使用Node.js Mosca进行身份验证和发布订阅

如果您的授权函数从不调用传入的callback,则它们将永远不会授权任何东西...

本文链接:https://www.f2er.com/2291815.html

大家都在问