简体   繁体   中英

Using Paho to connect to Mosquito MQTT server

I am trying to use Paho's JavaScript library to connect to a Mosquito MQTT server. I have just used the example on the Paho website. Which is:

function onConnect() {
  console.log("onConnect");
  client.subscribe("harleyRowland/myTopic");
  message = new Paho.MQTT.Message("Hello");
  message.destinationName = "harleyRowland/myTopic";
  client.send(message); 
}
onConnect();

On Safari, I get the following error:

Error: AMQJS0011E Invalid state not connected.

and on Chrome, I get the following error as well as the one above:

WebSocket connection to 'ws://mosquitoServer.co.uk:1883/mqtt' failed: Error during WebSocket handshake: net::ERR_CONNECTION_RESET

I need to use the Mosquito server for this work. Is it possible using JavaScript? If it isn't possible, is there another web technology I could use?

You need to add a websockets listener to your mosquitto setup. you do this by adding something like the following to your mosquitto.conf

listener 1884
protocol websockets

This will listen for MQTT over websockets on port 1884 (not 1883 as you can not run both on the same socket with mosquitto).

You will also need to be running mosquitto newer than 1.4 and not on Windows unless you compile it yourself

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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