我在CentOS 7上构建了mosquitto,并在一个基于qazxsw poi的node.js客户端上安装了
mqtt.js
本地测试
yum install mosquitto mosquitto-clients
工作正常,但当我跑:
> mosquitto_sub -h localhost -t test
> mosquitto_pub -h localhost -t test -m "hello world"
我收到错误:连接被拒绝:未经授权
mosquitto.conf就像:
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()
})
我使用systemctl重启mosquitto几次重启它,这没有帮助。防火墙已关闭,日志文件保持空白。状态截图:pid_file /var/run/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
log_dest file /var/log/mosquitto/mosquitto.log
allow_anonymous true
有人可以帮忙吗?
更新:
事实证明,蚊子服务在某种程度上被打破,因为状态显示。我使用Active: active (exited)
cmd在1884端口运行另一个mosquitto进程,它工作正常。然后我尝试使用mosquitto -p 1884 -v
重新加载conf。它给了我
重新加载mosquitto配置(通过systemctl):mosquitto.service的作业无效。 [FAILED]
所以蚊子服务有问题。不是最终解决方案,但我设法通过remove-reboot-install过程修复此问题,状态变为绿色,如下所示:
> /etc/init.d/mosquitto reload
解
我设法找出它不起作用的原因。我在我的服务器上安装了rabbitmq,它使用了“rabbitmq_mqtt”,它消耗了端口1883.重新分配端口将解决这个问题。
我设法找出原因。我在我的服务器上安装了rabbitmq,它使用了“rabbitmq_mqtt”,它消耗了端口1883.重新分配端口将解决这个问题。问题很简单,但是,CLI应该给我更多信息。
您需要将授权信息添加到mqtt connect方法。就像这样。
var mqtt = require('mqtt')
var client = mqtt.connect('mqtt://192.168.1.70', {
username: '<username>',
password: '<password>'
});
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()
})