-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_emqx_other.html
48 lines (38 loc) · 1.51 KB
/
index_emqx_other.html
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
38
39
40
41
42
43
44
45
46
47
48
<html lang="">
<head>
</head>
<body>
<p>HELLO</p>
<p id='demo'></p>
<script src="https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.1/mqttws31.js" type="text/javascript"></script>
<script>
var client = new Paho.MQTT.Client("localhost", Number(8083), "clientId");
var doc = document.getElementById("demo");
// 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("mqtt/demo");
console.log("subscribed");
}
// 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("Got msg");
var json = JSON.parse(message.payloadString);
var curr_time = new Date().getTime();
doc.innerHTML = (curr_time - json.timestamp) + " ms";
}
// set callback handlers
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
// connect the client
client.connect({onSuccess:onConnect});
</script>
</body>
</html>