簡體   English   中英

無法通過Websocket連接到Mosquitto

[英]Unable to connect to Mosquitto over Websocket

我無法通過Websocket的JavaScript客戶端連接到本地Mosquitto 1.4.10代理。

相同的JavaScript客戶端通過Websocket在端口8080的test.mosquitto.org上成功連接到公共代理。

端口1883上的MQTT協議連接工作正常,我使用mosquitto_pub和mosquitto_sub進行了測試。

我的代理是在運行Ubuntu 14.04的VirtualBox中設置的。

我在同一虛擬機上安裝了libwebsockets。

我的本地代理是使用config.mk文件中的WITH_WEBSOCKETS := yes編譯的

我正在從Firefox瀏覽器的同一虛擬機加載JavaScript客戶端網頁,並在瀏覽器控制台中看到以下錯誤消息:

Firefox無法在ws:// localhost:8080 / mqtt建立與服務器的連接

您對解決此問題的建議將不勝感激。

謝謝。

這是我的Mosquitto .conf文件:

port 1883
listener 8080
protocol websockets

log_type all
websockets_log_level 1023
connection_messages true

這是Mosquitto服務器的日志(websockets日志記錄級別設置為1023,並且詳細日志記錄已打開-加載JavaScript網頁時未顯示任何消息):

1481381105:mosquitto版本1.4.10(生成日期2016-12-10 18:47:37 + 0530)開始
1481381105:從/etc/mosquitto/mosquitto.conf中加載配置。
1481381105:打開websockets監聽端口8080上的套接字。
1481381105:初始日志記錄級別1023

1481381105:libwebsockets版本:2.1.0 manavkumarm @ manav-alljoyn

1481381105:未在其中編譯IPV6
1481381105:未編譯libev支持
1481381105:未編譯libuv支持
1481381105:線程:每個1024 fds 1
1481381105:mem:平台fd映射:4096字節
1481381105:編譯了OpenSSL支持
1481381105:創建Vhost“默認”端口8080、3個協議,關閉IPv6
1481381105:使用非SSL模式
1481381105:在端口8080上偵聽
1481381105:mem:每個conn:376個字節+協議rx buf
1481381105:canonical_hostname = mqtt
1481381105:在端口1883上打開ipv4偵聽套接字。
1481381105:在端口1883上打開ipv6偵聽套接字。

這是JavaScript源代碼:

 <html> <body> <script src="mqttws31.js"></script> <script> try { // Create a client instance console.log("Creating client object..."); client = new Paho.MQTT.Client("localhost", Number(8080), "manav"); //client = new Paho.MQTT.Client("test.mosquitto.org", Number(8080), "manav"); // set callback handlers console.log("Setting handlers..."); client.onConnectionLost = onConnectionLost; client.onMessageArrived = onMessageArrived; // connect the client console.log("Connecting..."); client.connect( { onSuccess: onConnect, mqttVersion: 4 }); } catch (e) { console.log("Error: " + e.description); } // called when the client connects function onConnect() { // Once a connection has been made, make a subscription and send a message. console.log("Connected"); setTimeout( function() { client.subscribe("world"); message = new Paho.MQTT.Message("Hello"); message.destinationName = "world"; client.send(message); //client.disconnect(); }, 5000); } // called when the client loses its connection function onConnectionLost(responseObject) { if (responseObject.errorCode !== 0) { console.log("Connection lost: " + responseObject.errorMessage); } } // called when a message arrives function onMessageArrived(message) { console.log("Received Message: " + message.payloadString); client.disconnect(); } </script> <h1>My MQTT Websockets Example</h1> </body> </html> 

我看到我的回答有點遲了。

MQTT的websocket端口是1884或其他,您有8080。也許就是問題所在。

8080是否是保留的TCP端口?

另外,我知道您有javascript代碼,但它的paho。 我能夠使發布者(它使用與訂閱者相同的類,因此它也必須在訂閱者方面-盡管這只是假設)在帶有paho python客戶端的websocket上工作,該客戶端必須使用定義的傳輸參數進行初始化。 ->與瀏覽器通信(有關JavaScript,請參見下文)

mqtt.Client(transport='websockets')

保留該參數MQTT假定使用TCP

mqtt.Client()

也:

在我的經紀人上配置:

# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example

pid_file /var/run/mosquitto.pid

persistence true
persistence_location /var/lib/mosquitto/

log_dest file /var/log/mosquitto/mosquitto.log

include_dir /etc/mosquitto/conf.d

listener 1883
listener 1884
protocol websockets

我用paho-mqtt找到了很老的Javascript。 它在工作,所以我把它放在這里。 它的訂戶和發布者同時。 與配置一起,它像魅力一樣工作。

class sub{
  constructor(hostname,port,clientid,topic){
    this.buffer = []

    this.hostname=hostname;
    this.port=port;
    this.clientid = clientid;
    this.topic = topic;
    this.client = new Paho.MQTT.Client(hostname,port, clientid);
    // set callback handlers
    this.client.onConnectionLost = this.onConnectionLost;
    this.client.onMessageArrived = this.onMessageArrived.bind(this);
    // connect the client
    this.client.connect({onSuccess:this.onConnect});
                                         }
onConnect(){
  console.log('OnConnect');
}

onConnectionLost(responseObject) {
  if (responseObject.errorCode !== 0) {
    console.log("onConnectionLost:"+responseObject.errorMessage);
}
}
onMessageArrived(message) {
  console.log("onMessageArrived:"+message.payloadString);
  this.buffer.push(JSON.parse(message.payloadString));


}


subsribe(){
this.client.subscribe(this.topic)
}
publish(message){
console.log(message)
var mg = new Paho.MQTT.Message(JSON.stringify(message));
mg.destinationName = this.topic;
this.client.send(mg);
}


}
var x
x = new sub('xx.xx.xx.xx',1884,'clientID','LED');

function on(){

x.publish({'LED':'ON'});
}
function off(){

x.publish({'LED':'OFF'});
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM