Paho Javascript Mqtt客户端(通过Websockets)未连接到Mosquitto Broker(托管在Google Cloud VM上)

时间:2019-08-08 17:08:17

标签: javascript mqtt mosquitto paho

我通过以下链接在VM上安装了Mosquitto https://gist.github.com/smoofit/dafa493aec8d41ea057370dbfde3f3fc

蚊子经纪人出现并显示

1565283316: mosquitto version 1.4.12 (build date 2019-08-08 15:47:50+0000) starting
1565283316: Config loaded from /etc/mosquitto/mosquitto.conf.
1565283316: Opening websockets listen socket on port 9001.
1565283316: Opening ipv4 listen socket on port 1883.
1565283316: Opening ipv6 listen socket on port 1883.

但无法从Paho Javascript客户端连接

我尝试用mosquitto安装最新版本的Mqtt。我在防火墙上允许9001和1883端口,但无法连接。

这是我的JS客户代码。

<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.1/mqttws31.min.js" type="text/javascript"></script>
</head>

<script>
// Create a client instance
client = new Paho.MQTT.Client(IPAddress, Number(9001), "clientId");

// set callback handlers
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;

// connect the client
client.connect({onSuccess:onConnect});


// called when the client connects
function onConnect() {
  // Once a connection has been made, make a subscription and send a message.
  console.log("onConnect");
  client.subscribe("World");
  message = new Paho.MQTT.Message("Hello");
  message.destinationName = "World";
  client.send(message);
}

// called when the client loses its connection
function onConnectionLost(responseObject) {
  if (responseObject.errorCode !== 0) {
    console.log("onConnectionLost:"+responseObject.errorMessage);
  }
}

// called when a message arrives
function onMessageArrived(message) {
  console.log("onMessageArrived:"+message.payloadString);
}
</script>
</html>

请帮助我解决此问题。预先感谢

0 个答案:

没有答案