如何捕捉“访问错误"何时在 mqtt.js 中发布无法访问的主题? [英] How to catch "access error" when publish inaccessible topic in mqtt.js?

查看:220
本文介绍了如何捕捉“访问错误"何时在 mqtt.js 中发布无法访问的主题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 node.js 和 mqtt.js 和 mosquitto 代理创建一个项目.在 mosquitto 配置文件中,我设置了一个 pwfile、aclfile 来控制哪个用户可以访问哪个主题.

I am creating a project using node.js with mqtt.js and mosquitto broker. In mosquitto config file, I have setup a pwfile, aclfile to control which topic can be accessed by which user.

一切正常,如果用户名、密码、发布主题和订阅主题正确.

Everything works fine, if the username, password, publish topic and subscribe topic are correct.

但是如果我将发布主题更改为无法访问的主题,在 mqtt.js 中似乎成功发布主题而没有任何错误,但消息从未发布.

But if I change the publish topic to an inaccessible topic, it seems successfully publish the topic without any error in mqtt.js, but the message is never been publish.

在发布或订阅无法访问的主题时是否有任何捕获错误?

Is there anyway to catch error when publish or subscribe to inaccessible topic?

https://github.com/mqttjs/MQTT.js/blob/master/README.md#publish

我尝试使用 qos=1 捕获回调函数中的错误,但似乎 mosquitto 代理确认客户端没有任何错误.当然,消息没有发布到主题,因为该客户端没有访问该主题的权限.

I try to catch the error in callback function with qos=1, but it seems the mosquitto broker acknowledge the client without any error. Of cause, the message didn't publish to the topic, since that client don't have the access right to that topic.

client.publish('inaccessible_topic', 'hello world', {qos: 1}, (err) => {
  if (err) console.log('error occur: ', err);
  else console.log('message successfully publish');
});

我希望 mosquitto 在发布到无法访问的主题时会返回某种错误(error发生:<error message>),但它没有(消息成功发布)代码>).

I expect mosquitto will return some sort of error (error occur: <error message>) when publish to the inaccessible topic, but it didn't (message sucessfully publish).

我想知道是否有可能捕捉到这样的错误.mosquitto 是否处理此类错误,如果是,我如何使用 mqtt.js 捕获此类错误?

I wonder if it is even possible to catch such error. Does mosquitto handle such error, if so, how do I catch such error using mqtt.js?

推荐答案

你没有.

broker 不会告诉客户端它是否尝试发布到它无权访问的主题,它只会默默地丢弃消息.

The broker will not tell the client if it tried to publish to a topic it doesn't have access to, it will just silently just drop the message.

这将是一个安全漏洞,因为它会暴露哪些主题可用.

This would be a security vulnerability in that it would expose what topics are available.

这篇关于如何捕捉“访问错误"何时在 mqtt.js 中发布无法访问的主题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆