Friday, February 23, 2024
 Popular · Latest · Hot · Upcoming
77
rated 0 times [  84] [ 7]  / answers: 1 / hits: 7001  / 9 Years ago, fri, january 2, 2015, 12:00:00

I am trying simple websocket based MQTT client and having Uncaught ReferenceError: Messaging is not defined error on console of Chrome.



<!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 is on line var client = new Messaging.Client(wsbroker, wsport,



Error os Uncaught ReferenceError: Messaging is not defined



The java Script is from http://git.eclipse.org/c/paho/org.eclipse.paho.mqtt.javascript.git/tree/src/mqttws31.js



Please suggest any solution...


More From » websocket

 Answers
25

You are using an old example, the package name has changed it's not Messaging anymore it should be:



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


EDIT:
Also the port number for test.mosquitto.org is 8080 not 80


[#40288] Wednesday, December 31, 2014, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ronaldo

Total Points: 694
Total Questions: 85
Total Answers: 103

Location: Somalia
Member since Mon, Feb 27, 2023
1 Year ago
;