简体   繁体   English

MQTT mosquitto - 使用mqttws31.js在javascript中实现websocket

[英]MQTT mosquitto - Implementing websocket in javascript using mqttws31.js

Implementing a websocket using MQTT with mosquitto broker using javascript by including mqttws32.js file. 使用MQTT和mosquitto代理使用javascript实现websocket,包括mqttws32.js文件。

Mosquitto version: 1.4.9 Mosquitto版本:1.4.9

On start I got: 一开始我得到了:

rohan@rohan-PC:~$ mosquitto
1470241326: mosquitto version 1.4.9 (build date Fri, 03 Jun 2016 09:22:47 +0100) starting
1470241326: Using default config.
1470241326: Opening ipv4 listen socket on port 1883.
1470241326: Opening ipv6 listen socket on port 1883.

As latest version of mosquitto supports Web socket. 由于最新版本的mosquitto支持Web套接字。

I'm using following code: 我正在使用以下代码:

client = new Paho.MQTT.Client("localhost", 1883, "myclientid_" + parseInt(Math.random() * 100, 10));

Getting error: 得到错误:

WebSocket connection to 'ws://localhost:1883/mqtt' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED 与'ws:// localhost:1883 / mqtt'的WebSocket连接失败:连接建立错误:net :: ERR_CONNECTION_REFUSED

I listener i got: 我听到了:

1470241581: New connection from ::1 on port 1883.
1470241581: Socket error on client <unknown>, disconnecting.

After searching I found a solution to add following line to mosquitto.conf file 搜索后,我找到了一个解决方案,将以下行添加到mosquitto.conf文件中

listener 1884
protocol websockets

I changed to port to 1884 but still getting the same error. 我改为端口到1884但仍然得到相同的错误。

Mosquitto does not have a default config file location so you need to actually specify it on the command line Mosquitto没有默认的配置文件位置,因此您需要在命令行上实际指定它

$ mosquitto -c /path/to/mosquitto.conf

The config file also should have an extra line in it if you still want to listen on 1883 for native MQTT connections as well as websockets on 1884 如果您仍希望在1883年侦听本机MQTT连接以及1884年的websockets,那么配置文件中还应该有一行额外的行

port 1883

listener 1884
protocol websockets

That should get you a functioning broker that is actually listening for MQTT over Websockets on port 1884 这应该会让你成为一个正在运行的代理,实际上是在端口1884上通过Websockets监听MQTT

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

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