-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
48 lines (41 loc) · 1.37 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
<!DOCTYPE html>
<html>
<body>
<h1>Getting server updates</h1>
<button id="closeButton" type="button">Close SSE</button>
<div id="result"></div>
<script>
let source;
const resultElement = document.getElementById("result");
const HOST = "http://localhost";
if (typeof EventSource !== "undefined") {
source = new EventSource(`${HOST}:8080/sse?query=7`);
source.onerror = (err) => {
console.log("on error", err);
};
// The onmessage handler is called if no event name is specified for a message.
source.onmessage = (msg) => {
console.log("on message", msg);
};
source.onopen = (...args) => {
console.log("on open", args);
};
source.addEventListener("current-value", (event) => {
const parsedData = JSON.parse(event.data);
const currentValue = parsedData.data;
console.log(currentValue);
resultElement.innerHTML = currentValue + "<br>";
});
} else {
resultElement.innerHTML = "Your browser does not support SSE";
}
document.getElementById("closeButton").onclick = function () {
if (source) {
console.log("Stop SSE");
// If the connection is already closed, the method does nothing.
source.close();
}
};
</script>
</body>
</html>