-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
69 lines (62 loc) · 1.79 KB
/
index.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<html>
<head>
<title>web-socket-mqtt</title>
<style type="text/css">
#log {
width: 95%;
height: 400px;
overflow: auto;
border: 1px solid burlywood;
}
</style>
</head>
<body>
<h2>Send</h2>
<div>
<div>
<label for="topic">Topic</label>
<input type="text" id="topic" value="topic1" />
</div>
<div>
<label for="subject">Subject</label>
<input type="text" id="subject" value="subject" />
</div>
<div>
<button type="button" onclick="send()">Send</button>
</div>
</div>
<h2>Log</h2>
<ul id="log"></ul>
<script src="./mqtt"></script>
<script>
console.log(document.location.protocol);
var client = mqtt.connect(
(document.location.protocol === "http:" ? "ws" : "wss") +
"://" +
document.location.hostname +
"/mqtt"
);
client.subscribe("log");
client.on("message", function(topic, message) {
context = message.toString();
console.log(topic, context);
var log = document.getElementById("log");
var entry = document.createElement("li");
entry.innerText = [topic, context].join(": ");
log.appendChild(entry);
var entries = log.getElementsByTagName("li");
while (entries.length >= 1000) {
log.removeChild(entries[0]);
}
log.scrollTo(0, log.scrollHeight);
});
function send() {
var topic = document.getElementById('topic').value;
var subject = document.getElementById('subject').value;
console.log('send', topic, subject);
client.publish('log', 'sending to ' + topic + ': ' + subject);
client.publish(topic, subject);
}
</script>
</body>
</html>