2017-08-18 46 views
0

我建mosquitto上CentOS7和基於mqtt.js一個node.js的客戶端,與mosquitto + mqtt.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重啓mosquitto重啓了好時候,這不利於。防火牆關閉,日誌文件保持空白。 狀態截圖: enter image description here

任何人都可以幫忙嗎?

UPDATE:

事實證明,該mosquitto服務以某種方式打破的狀態顯示Active: active (exited)。 我使用mosquitto -p 1884 -v cmd在1884港口運行另一個蚊子過程,它工作正常。然後我嘗試使用 > /etc/init.d/mosquitto reload重新加載conf。它給我

重新加載mosquitto配置(通過systemctl):作業mosquitto.service無效。 [FAILED]

所以蚊子服務有問題。 不是最終解決方案,但我設法通過解決這一刪除,重新啓動安裝過程中,狀態又綠如下:

Correct mosquitto status

SOLUTION

我設法找出原因,不起作用。我在我的服務器上安裝了rabbitmq,它使用其「rabbitmq_mqtt」,它使用端口1883.重新分配端口將解決此問題。

+0

中有什麼mosquitto日誌當客戶端的NodeJS嘗試連接? – hardillb

+0

正如我所說的......可怕的是,日誌是空的,不知道日誌功能是否無法運行。 – joe

+0

停止mosquitto並在cmd行上用'-v'手動運行它 – hardillb

回答

-1

您需要將授權信息添加到mqtt connect method.Just是這樣的。

var client=mqtt.connect("ws://192.168.1.1", { 
 
      username: "yourUsername", 
 
      password: "yourPassword" 
 
     }

+0

你好,歡迎來到StackOverflow!請查看[如何提問](https://stackoverflow.com/help/how-to-ask)指南,獲取有關詢問優秀和明確問題的提示。 – clabe45

+0

但我設置匿名true,它應該允許匿名連接。 – joe