简体   繁体   English

Paho客户端Web套接字找不到mqtt

[英]Paho Client Web Socket Can't Find mqtt

I've been given a project that is using a Paho/MQTT client through Javascript to update a web page. 我得到了一个通过Java使用Paho / MQTT客户端来更新网页的项目。 The project starts an http server through python on localhost:8080 and then, when the webpage is loaded, a main.js script starts the client and runs the connect as shown below: 该项目通过localhost:8080上的python启动http服务器,然后在加载网页时,main.js脚本启动客户端并运行连接,如下所示:

var client = new Paho.MQTT.Client("localhost",Number(8080),'0');
client.onConnectionLost = onConLost;
client.onMessageArrived = onMesArvd;

console.log("start connection...");
client.connect({onSuccess: onConnect});

The problem is that upon trying to connect the below error appears and the onConnect method does not appear to connect as a console.log does not appear: 问题是尝试连接时出现以下错误,并且onConnect方法似乎未作为控制台进行连接。日志未出现:

WebSocket connection to 'ws://localhost:8080/mqtt' failed: Error during WebSocket handshake: Unexpected response code: 404

The code highlighted in red in the paho-mqtt.js is below: paho-mqtt.js中以红色突出显示的代码如下:

new WebSocket(a, ["mqtt"])

I tried adding a mqtt file to the folder localhost is being run from but it only changes the response code (301 if a mqtt folder is present, 200 if a file). 我尝试将mqtt文件添加到要从其运行localhost的文件夹中,但它仅更改响应代码(如果存在mqtt文件夹,则为301,如果存在文件,则为200)。

Adding the mosquitto.conf that came with the project: 添加项目随附的mosquitto.conf:

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 8080
protocol websockets

I noticed it's not in the same folder as the paho/mqtt client but in ../dir2/setup/. 我注意到它与paho / mqtt客户端不在同一个文件夹中,而在../dir2/setup/中。

I've looked around but I haven't seen anyone with this issue so any help/guidance would be greatly appreciated. 我环顾四周,但还没有人遇到这个问题,因此对您的帮助/指导将不胜感激。 Thanks! 谢谢!

Thanks to Santosh Balaji for pointing me in the right direction on this one. 感谢Santosh Balaji在这个问题上为我指明了正确的方向。

I believe I needed to install mosquitto on the pi and then change the mosquitto.conf file in the mosquitto install dir to the one provided by the project. 我相信我需要在pi上安装mosquitto ,然后将mosquitto安装目录中的mosquitto.conf文件更改为项目提供的文件。 After installing and confirming my conf was being used the js and Python connected without issue. 安装并确认我的conf被使用后,js和Python连接没有问题。

1) Is port 8080 occupied by other process before starting mqtt. 1)启动mqtt之前,端口8080是否被其他进程占用。 Try to start your mosquitto with conf file. 尝试使用conf文件启动您的蚊子。 It will show up error if there is anything wrong with the start. 如果开始有任何错误,它将显示错误。

mosquitto -c mosquitto.conf mosquitto -c mosquitto.conf

2) Try changing the port to 9001 as it is default port for using websockets in mqtt 2)尝试将端口更改为9001,因为它是在mqtt中使用websockets的默认端口

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM