-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadPage1.html
48 lines (41 loc) · 1.49 KB
/
loadPage1.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
<!DOCTYPE html>
<html>
<head>
<title>WebSocket Livestream Client</title>
</head>
<body>
<h1>WebSocket Livestream</h1>
<canvas id="livestreamCanvas" width="320" height="240"></canvas>
<script>
var webSocket;
var canvas = document.getElementById("livestreamCanvas");
var ctx = canvas.getContext("2d");
function connectWebSocket() {
webSocket = new WebSocket("ws://localhost:8080/");
webSocket.binaryType = 'arraybuffer';
webSocket.onopen = function () {
console.log("WebSocket connection established.");
sendInitialData();
};
webSocket.onmessage = function (event) {
var imageData = new Uint8Array(event.data);
var image = new Image();
image.onload = function () {
ctx.drawImage(image, 0, 0);
};
image.src = 'data:image/jpeg;base64,' + btoa(String.fromCharCode.apply(null, imageData));
sendInitialData();
};
webSocket.onclose = function () {
console.log("WebSocket connection closed.");
};
}
function sendInitialData() {
// Send a small amount of initial data to the server
var initialData = new Uint8ClampedArray(10);
webSocket.send(initialData);
}
connectWebSocket();
</script>
</body>
</html>