【发布时间】:2017-08-18 02:06:21
【问题描述】:
我在CentOS7上搭建了mosquitto和一个基于mqtt.js的node.js客户端,使用安装
yum install mosquitto mosquitto-clients
本地测试
> mosquitto_sub -h localhost -t test
> mosquitto_pub -h localhost -t test -m "hello world"
工作正常,但是当我运行时:
var mqtt = require('mqtt')
var client = mqtt.connect('mqtt://192.168.1.70')
client.on('connect', function () {
client.subscribe('presence')
client.publish('presence', 'Hello mqtt')
})
client.on('message', function (topic, message) {
// message is Buffer
console.log(message.toString())
client.end()
})
我得到错误:连接被拒绝:未授权
mosquitto.conf 是这样的:
pid_file /var/run/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
log_dest file /var/log/mosquitto/mosquitto.log
allow_anonymous true
我使用 systemctl restart mosquitto 重新启动了几次,这没有帮助。防火墙已关闭,日志文件保持为空。 状态截图:
有人可以帮忙吗?
更新:
事实证明,mosquitto 服务在某种程度上被破坏了,因为状态显示为Active: active (exited)。
我使用mosquitto -p 1884 -v cmd 在端口 1884 上运行另一个 mosquitto 进程,它工作正常。然后我尝试使用重新加载conf
> /etc/init.d/mosquitto reload。它给了我
重新加载 mosquitto 配置(通过 systemctl):mosquitto.service 的作业无效。 [失败]
所以蚊子服务有问题。 不是最终解决方案,但我设法通过 remove-reboot-install 过程解决了这个问题,状态变为绿色如下:
解决方案
我设法找出它不起作用的原因。我在我的服务器上安装了rabbitmq,它使用它的“rabbitmq_mqtt”,它消耗端口1883。重新分配一个端口将解决这个问题。
【问题讨论】:
-
NodeJS 客户端尝试连接时 mosquitto 日志中的内容是什么?
-
正如我所说..奇怪的是日志是空的,不确定日志功能是否无法运行。
-
停止 mosquitto 并在 cmd 行仅使用
-v手动运行它 -
好吧,如果我使用 mosquitto -v 在端口 1884 上启动另一个 mosquitto 进程,一切正常。但不是 mosquitto 服务,它仍然显示在端口 1883 上未授权。
标签: javascript node.js mqtt mosquitto