蚊子+mqtt.js 得到了 "Connection refused: Not authorized"
mosquitto+mqtt.js got "Connection refused: Not authorized"
我在 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 的作业无效。
[失败]
所以 mosquitto 服务有问题。
不是最终的解决方案,但我设法通过删除-重新启动-安装过程解决了这个问题,状态变为绿色如下:
解决方案
我设法找出它不起作用的原因。我在我的服务器上安装了 rabbitmq,它使用它的 "rabbitmq_mqtt" 消耗端口 1883。重新分配一个端口将解决这个问题。
您需要像这样将授权信息添加到mqtt connect method.Just。
var client=mqtt.connect("ws://192.168.1.1", {
username: "yourUsername",
password: "yourPassword"
}
添加客户端连接的授权详细信息
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()
})
我设法找到了原因。我在我的服务器上安装了 rabbitmq,它使用它的 "rabbitmq_mqtt" 消耗端口 1883。重新分配一个端口将解决这个问题。问题很简单,但是,是的,CLI 应该给了我更多信息。
我在 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 的作业无效。 [失败]
所以 mosquitto 服务有问题。 不是最终的解决方案,但我设法通过删除-重新启动-安装过程解决了这个问题,状态变为绿色如下:
解决方案
我设法找出它不起作用的原因。我在我的服务器上安装了 rabbitmq,它使用它的 "rabbitmq_mqtt" 消耗端口 1883。重新分配一个端口将解决这个问题。
您需要像这样将授权信息添加到mqtt connect method.Just。
var client=mqtt.connect("ws://192.168.1.1", {
username: "yourUsername",
password: "yourPassword"
}
添加客户端连接的授权详细信息
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()
})
我设法找到了原因。我在我的服务器上安装了 rabbitmq,它使用它的 "rabbitmq_mqtt" 消耗端口 1883。重新分配一个端口将解决这个问题。问题很简单,但是,是的,CLI 应该给了我更多信息。