-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
70 lines (58 loc) · 1.65 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<!DOCTYPE html>
<script>
let eventSource;
// when "Start" button pressed
function start() {
if (!window.EventSource) {
// IE or an old browser
alert("The browser doesn't support EventSource.");
return;
}
eventSource = new EventSource('digits');
eventSource.addEventListener('aloha', function (e) {
log("Event: aloha sam aldi, data: " + e.data);
});
eventSource.onopen = function (e) {
log("Event: on open");
};
eventSource.onerror = function (e) {
log("Event: on error");
if (this.readyState == EventSource.CONNECTING) {
log(`Reconnecting (readyState=${this.readyState})...`);
} else {
log("Error has occured.");
}
};
eventSource.addEventListener('bye', function (e) {
log("Event: bye, data: " + e.data);
});
eventSource.onmessage = function (e) {
log("Event: message, data: " + e.data);
};
}
// when "Stop" button pressed
function stop() {
eventSource.close();
log("eventSource.close()");
}
// when "Reset" button pressed
function reset() {
eventSource.close();
logElem.innerHTML = "";
log("event closed, Log cleared.");
}
function log(msg) {
logElem.innerHTML += msg + "<br>";
document.documentElement.scrollTop = 99999999;
}
</script>
<button onclick="start()">Start</button> Press the "Start" to begin.
<div id="logElem" style="margin: 6px 0"></div>
<div style="display: flex; flex-direction: column; row-gap: 6px;">
<div>
<button onclick="stop()">Stop</button> "Stop" to finish.
</div>
<div>
<button onclick="reset()">Reset</button> "Reset" to clear the log.
</div>
</div>