-
Notifications
You must be signed in to change notification settings - Fork 0
/
MQTT_Receive.html
40 lines (33 loc) · 919 Bytes
/
MQTT_Receive.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
<html>
<head>
<title>MQTT.Js Receive</title>
<script src="js/mqtt.min.js"></script>
</head>
<body>
<button onclick="startConnect()">Start Connection</button>
<button onclick="stopConnect()">Stop Connection</button>
<script>
var client;
var brokerUrl = "http://127.0.0.1:15675/ws";
var topicName = "topic/mcs-test";
var opts = {
username: "YourUserName",
password: "YourPassword"
};
function startConnect() {
client = mqtt.connect(brokerUrl); // (brokerUrl, opts) #if auth required
client.on('connect', () => {
console.log("CONNECT SUCCESS");
client.subscribe(topicName);
});
client.on("message", function (topic, payload) {
console.log("[x] Received: %s", payload.toString());
});
}
function stopConnect() {
client.end();
console.log("CONNECTION STOP");
}
</script>
</body>
</html>