-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_image_receive_websockets.html
62 lines (48 loc) · 1.67 KB
/
test_image_receive_websockets.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WebSocket Image Display</title>
</head>
<body>
<canvas id="imageCanvas" width="800" height="600"></canvas>
<script>
const host = "greenlab.unibo.it"; // Replace with the actual IP address or hostname
const port = 443;
const path_ws = "ws"
const path = "cam"
let deviceName = 'CAM-eli'; // Replace with the actual device name
const canvas = document.getElementById('imageCanvas');
const ctx = canvas.getContext('2d');
let ws;
function initWebSocket() {
const wsUrl = `wss://${host}:${port}/${path_ws}/${path}/${deviceName}`;
ws = new WebSocket(wsUrl);
ws.binaryType = 'arraybuffer';
ws.onopen = function() {
console.log('WebSocket connected');
};
ws.onmessage = function(event) {
const imageBlob = new Blob([event.data], { type: 'image/jpeg' });
const imageUrl = URL.createObjectURL(imageBlob);
loadImage(imageUrl);
};
ws.onclose = function() {
console.log('WebSocket closed');
};
}
function loadImage(imageUrl) {
const img = new Image();
img.onload = function() {
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
};
img.src = imageUrl;
}
// Initialize WebSocket connection when the page loads
window.onload = function() {
initWebSocket();
};
</script>
</body>
</html>