简体   繁体   English

使用Paho连接到Mosquito MQTT服务器

[英]Using Paho to connect to Mosquito MQTT server

I am trying to use Paho's JavaScript library to connect to a Mosquito MQTT server. 我正在尝试使用Paho的JavaScript库连接到Mosquito MQTT服务器。 I have just used the example on the Paho website. 我刚刚在Paho网站上使用了该示例。 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: 在Safari上,出现以下错误:

Error: AMQJS0011E Invalid state not connected. 错误:AMQJS0011E未连接无效状态。

and on Chrome, I get the following error as well as the one above: 在Chrome上,我收到以下错误以及上述错误:

WebSocket connection to 'ws://mosquitoServer.co.uk:1883/mqtt' failed: Error during WebSocket handshake: net::ERR_CONNECTION_RESET WebSocket与“ ws://mosquitoServer.co.uk:1883 / mqtt”的连接失败:WebSocket握手期间出错:net :: ERR_CONNECTION_RESET

I need to use the Mosquito server for this work. 我需要使用Mosquito服务器来完成这项工作。 Is it possible using JavaScript? 是否可以使用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. 您需要在mosquitto设置中添加一个websockets侦听器。 you do this by adding something like the following to your mosquitto.conf 您可以通过将以下内容添加到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). 这将通过端口1884上的websocket侦听MQTT(不是侦听MQTT,因为您不能同时在mosquitto的同一套接字上运行这两者)。

You will also need to be running mosquitto newer than 1.4 and not on Windows unless you compile it yourself 您还需要运行高于1.4的mosquitto,而不是在Windows上运行,除非您自己进行编译

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

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