2015-01-02 244 views
2

我想尝试简单的基于websocket的MQTT客户端,并在Chrome的控制台上出现“Uncaught ReferenceError:Messaging is not defined”错误。在MQTT客户端“未捕获的ReferenceError:消息未定义”

<!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 --> 

ERROR是上线 “变种客户=新Messaging.Client(wsbroker,wsport,”

错误OS “未捕获的ReferenceError:未定义消息”

的Java脚本从http://git.eclipse.org/c/paho/org.eclipse.paho.mqtt.javascript.git/tree/src/mqttws31.js

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

+0

尝试从第一个脚本标记中移除异步推迟标记 – hardillb

+0

仍然相同... @hardillb –

回答

7

您使用的是旧例如,包名称已更改它不是Messag荷兰国际集团不再应该是:

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

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

1

我一直在四处寻找一个例子,尝试了上面的代码建议的编辑,但它仍然失败...(也许mqttws31.js已经改变),但我从here得到了一个工作示例,当您将端口更改为8080时,它工作得很好!

- 请帮助某人!

<!DOCTYPE html> 
<html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/> 
    <script src="js/mqttws31.js" type="text/javascript"></script> 
    <script type="text/javascript"> 
    //sample HTML/JS script that will publish/subscribe to topics in the Google Chrome Console 
    //by Matthew Bordignon @bordignon on twitter. 
    var wsbroker = "test.mosquitto.org"; //mqtt websocket enabled brokers 
    var wsport = 8080 // port for above 
    var client = new Paho.MQTT.Client(wsbroker, wsport, 
     "myclientid_" + parseInt(Math.random() * 100, 10)); 
    client.onConnectionLost = function (responseObject) { 
     console.log("connection lost: " + responseObject.errorMessage); 
    }; 
    client.onMessageArrived = function (message) { 
     console.log(message.destinationName, ' -- ', message.payloadString); 
    }; 
    var options = { 
     timeout: 3, 
     onSuccess: function() { 
     console.log("mqtt connected"); 
     // Connection succeeded; subscribe to our topic, you can add multile lines of these 
     client.subscribe('/World', {qos: 1}); 

     //use the below if you want to publish to a topic on connect 
     message = new Paho.MQTT.Message("Hello"); 
     message.destinationName = "/World"; 
     client.send(message); 

     }, 
     onFailure: function (message) { 
     console.log("Connection failed: " + message.errorMessage); 
     } 
    }; 
    function init() { 
     client.connect(options); 
    } 
    </script> 
    </head> 
    <body onload="init();"> 
    </body> 

</html> 
相关问题