繁体   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