javascript - 在 MQTT 客户端 "Uncaught ReferenceError: Messaging is not defined"

标签 javascript websocket mqtt

我正在尝试基于简单的 websocket 的 MQTT 客户端,并在 Chrome 控制台上出现“Uncaught ReferenceError: Messaging is not defined”错误。

<!DOCTYPE html>
<head>

  <head>
  <script src="mqttws31.js" type="text/javascript" charset="utf-8" async defer></script>


  <script type="text/javascript">

  var client;
  var form = document.getElementById("tutorial");

  function doConnect() {
        var wsbroker = "test.mosquitto.org";  //mqtt websocket enabled broker
    var wsport = 80 // port for above

    var client = new Messaging.Client(wsbroker, wsport,
        "myclientid_" + parseInt(Math.random() * 100, 10));
      client.onConnect = onConnect;
      client.onMessageArrived = onMessageArrived;
      client.onConnectionLost = onConnectionLost;
      client.connect({onSuccess:onConnect});
  }

  function doSubscribe() {
      client.subscribe("/World");
  }

  function doSend() {
      message = new Messaging.Message("Hello");
      message.destinationName = "/World";
      client.send(message);
  }

  function doDisconnect() {
      client.disconnect();
  }

  // Web Messaging API callbacks

  function onConnect() {
      var form = document.getElementById("example");
      form.connected.checked= true;
  }

  function onConnectionLost(responseObject) {
      var form = document.getElementById("example");
      form.connected.checked= false;
      if (responseObject.errorCode !== 0)
          alert(client.clientId+"\n"+responseObject.errorCode);
  }

  function onMessageArrived(message) {
      var form = document.getElementById("example");
      form.receiveMsg.value = message.payloadString;
  }

  </script>
</head> 

<body>
  <h1>Example Web Messaging web page.</h1>
  <form id="example">
  <fieldset>
  <legend id="Connect" > Connect </legend>
    Make a connection to the server, and set up a call back used if a 
    message arrives for this client.
    <br>
    <input type="button" value="Connect" onClick="doConnect(this.form)" name="Connect"/>
    <input type="checkbox" name="connected" disabled="disabled"/>
  </fieldset>

  <fieldset>
  <legend id="Subscribe" > Subscribe </legend>
  Make a subscription to topic "/World".
  <br> <input type="button" value="Subscribe" onClick="doSubscribe(this.form)"/>
  </fieldset>

  <fieldset>
  <legend id="Send" > Send </legend>
    Create a Message object containing the word "Hello" and then publish it at
    the server.
    <br>
    <input type="button" value="Send" onClick="doSend(this.form)"/>
  </fieldset>

  <fieldset>
  <legend id="Receive" > Receive </legend>
    A copy of the published Message is received in the callback we created earlier.
  <textarea name="receiveMsg" rows="1" cols="40" disabled="disabled"></textarea>
  </fieldset>

  <fieldset>
  <legend id="Disconnect" > Disconnect </legend>
    Now disconnect this client from the server.
  <br> <input type="button" value="Disconnect" onClick="doDisconnect()"/>
  </fieldset>
  </form>
</body>
</html -->

错误在线“var client = new Messaging.Client(wsbroker, wsport,”

错误 os “Uncaught ReferenceError: Messaging is not defined”

java脚本来自http://git.eclipse.org/c/paho/org.eclipse.paho.mqtt.javascript.git/tree/src/mqttws31.js

请提出任何解决方案...

最佳答案

您使用的是旧示例,包名称已更改,不再是 Messaging,它应该是:

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

编辑: test.mosquitto.org 的端口号也是 8080 而不是 80

关于javascript - 在 MQTT 客户端 "Uncaught ReferenceError: Messaging is not defined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27740003/

相关文章:

javascript - 自动复制隐藏文本

iphone - socket 火箭测试/教程

java - MqttAsyncClient 客户端未连接 (32104)

javascript - 如何将照片幻灯片居中

javascript - 有没有一种方法可以通过单击按钮来验证表单的一部分,而无需先实际提交表单?

javascript - 在 AngularJs 中传递变量值作为过滤器参数

javascript - 浏览器的 Websocket 库?

websocket - HTTP/2 是否会使 websockets 过时?

android - 适用于 Android 的 MQTT 代理

c++ - Mqtt 树莓派 C++