-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
56 lines (41 loc) · 1.12 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
49
50
51
52
53
54
55
56
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>SSE Hello world</title>
</head>
<body style="line-height: 2">
<input placeholder="name" id="name"/>
</br>
<button onclick="openStream()">open stream</button>
<button onclick="closeStream()">close stream</button>
</br>
<div>
Output: <output id="eventOutput"></output>
</div>
<script>
var eventSource = null;
function createEventSource() {
let name = document.getElementById('name').value;
if (!name.trim()) {
name = "john";
}
eventSource = new EventSource("http://localhost:8080/open-sse-stream/" + name);
eventSource.onmessage = function(event) {
console.log(event.data);
document.getElementById('eventOutput').value = event.data;
};
};
function closeStream() {
eventSource.close();
}
function openStream() {
if (eventSource !== null && eventSource.readyState !== EventSource.CLOSED) {
console.log("stream already opened");
return;
}
createEventSource();
}
</script>
</body>
</html>