-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
38 lines (31 loc) · 1.06 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SSE Client Demo</title>
</head>
<body>
<h1>Demo Client for SSE</h1>
<button type="button" id="triggerEvents">Trigger Event</button>
<ul style="font-size: 25px;"></ul>
<script>
function triggerEvent() {
fetch('http://127.0.0.1:8080/trigger', {
method: 'GET',
})
}
var btn = document.getElementById("triggerEvents");
btn.addEventListener("click", triggerEvent);
// Read SSE logic
const evtSource = new EventSource('http://127.0.0.1:8080/events?stream=messages');
const eventList = document.querySelector('ul');
evtSource.onmessage = (e) => {
const newElement = document.createElement("li");
newElement.textContent = `message: ${e.data}`;
eventList.appendChild(newElement);
}
</script>
</body>
</html>