Repository navigation
Expand file tree
/
Copy pathai.html
More file actions
67 lines (61 loc) · 1.51 KB
/
Copy pathai.html
File metadata and controls
67 lines (61 loc) · 1.51 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
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
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>MQTT Client Example</title>
<meta charset="utf-8">
<meta name="viewport" content="height=device-height">
<script src="https://unpkg.com/mqtt/dist/mqtt.min.js"></script>
<style>
div {
width: 100%;
height: 100%;
}
img#cameraView {
max-width: 100%;
max-height: 100%;
bottom: 0;
left: 0;
right: 0;
top: 0;
margin: auto;
overflow: auto;
position: fixed;
}
</style>
</head>
<body>
<h1>MQTT Client Example</h1>
<div align="center">
<img id="cameraView" width="100%" heigh="100%">
</div>
<script type="text/javascript">
const broker = 'ws://localhost:9001';
const topic = '/camera/objects';
const client = mqtt.connect(broker);
client.on("connect", () => {
console.log("Connected to broker");
client.subscribe(topic, (err) => {
if(!err) {
console.log(`Subscibed to topic: ${topic}`);
}
})
})
client.on("message", (topic, message) => {
try{
const payload = JSON.parse(message.toString());
const base64Image = payload.image;
const img = document.getElementById("cameraView")
img.src = `data:image/jpg;base64,${base64Image}`;
}catch(e) {
console.error('Failed to parse message: ', e);
}
})
client.on("error", (error) => {
console.error("connection error: ", error);
})
client.on("close", ()=> {
console.log("Disconnected from broker");
})
</script>
</body>
</html>