【问题标题】:paho js mqtt client fires onConnect eventpaho js mqtt 客户端触发 onConnect 事件
【发布时间】:2018-08-31 22:11:34
【问题描述】:

我正在使用 paho js 客户端连接到 mosquitto。它连接正常。但是 onConnect 事件正在不停地触发。我正在检查代理中的日志,看起来它只连接了一次。为什么会这样?我只是使用了官网上出现的代码:

 <html>
   <head>
      <title>JavaScript MQTT WebSocket Example</title>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/paho- mqtt/1.0.1/mqttws31.js" type="text/javascript">
     </script>
     <script type = "text/javascript" language = "javascript">
           var host = "192.168.1.200";
        var port = 1884;
        // Create a client instance
         client = new Paho.MQTT.Client(host, port, "abcd");

     // 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);
    console.log("messageSent");
}

// 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>
   </head>
     <body>
   <h1>Main Body</h1>
    <script>
    </script>
   </body>  
</html>

【问题讨论】:

  • 你有多少用户?
  • 只有一个。我正在 mosquitto 上测试 websocket 连接
  • 我在 github 上播种,其他人对蚊子 1.5 有同样的问题。但我没有找到任何解决方案
  • 快速浏览一些问题意味着这可能会在 Mosquitto 1.5.1 中得到修复,如果不降级到 1.4.15 可能是前进的方向。

标签: javascript client mqtt paho


【解决方案1】:

我通常会在我的客户端 ID(此处为“abcd”)已连接时注意到这一点。为了在多个设备上部署,我将其设为 UUID。

【讨论】:

    【解决方案2】:

    正如 samwell 所指出的,问题在于clientID

    如果两个客户端使用相同的 clientID,最后一个“踢出”之前连接的客户端。 我认为是因为 Mosquitto 为每个 clientID 维护一个 websocket。

    Paho JS 向onConnectionLost 回调报告以下两个错误之一:

    • 代码 7:AMQJS0007E 套接字错误:未定义
    • 代码 8:AMQJS0008I 套接字关闭

    为客户端使用唯一的 ID 或使用特定于用户的 ID。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-31
      • 1970-01-01
      • 1970-01-01
      • 2018-03-17
      • 2016-10-27
      • 2018-10-18
      • 1970-01-01
      相关资源
      最近更新 更多