-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathjsClient.js
More file actions
37 lines (31 loc) · 1.03 KB
/
Copy pathjsClient.js
File metadata and controls
37 lines (31 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
var client = new Paho.MQTT.Client("[url of pi]", Number(9001), "clientId" + Date.now().toString());
// set callback handlers
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
//Save current msgerature
var msg = " ";
//Options object for connection
var connect_options = {
timeout: 3,
onSuccess: function () {
// Connection succeeded; subscribe to our topic
console.log('Connected!');
client.subscribe('uicomm/p-j', {qos: 0});
},
onFailure: function (message) {
alert("Connection failed: " + message.errorMessage);
}
};
// connect the client
client.connect(connect_options);
// called when the client loses its connection
function onConnectionLost(responseObject) {
if (responseObject.errorCode !== 0) {
console.log("Connection Lost:"+responseObject.errorMessage);
}
}
// called when a message arrives
function onMessageArrived(message) {
console.log("Message Arrived:"+message.payloadString);
$("#msg_txt").html(message.payloadString);
}