-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
81 lines (72 loc) · 2.48 KB
/
index.html
File metadata and controls
81 lines (72 loc) · 2.48 KB
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
71
72
73
74
75
76
77
78
79
80
81
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>Hi</h1>
<button onclick="post()">Post</button>
<div id="load"></div>
<div id="eventList"></div>
<script type="text/javascript">
var events = [];
var evtSource;
var retries = 0;
var eventList = document.getElementById('eventList');
function add(data) {
const newElement = document.createElement('li');
const json = JSON.parse(data);
events.push(json);
if (events.length > 5) {
events.shift();
}
render();
document.getElementById('load').innerText = JSON.stringify(json.load);
}
function connect() {
console.log('New connect');
evtSource = new EventSource('/sse/stream');
evtSource.addEventListener('ping', function(event) {
add(event.data);
});
evtSource.addEventListener('open', function(event) {
console.log('Verbunden');
retries = 0;
});
// evtSource.addEventListener('error', function(err) {
// console.error(err);
// evtSource.close();
// reconnect();
// });
}
function reconnect() {
console.log('Retries', retries);
retries++;
let timeout = Math.pow(2, retries);
timeout = timeout > 16 ? 16 : timeout;
console.log(timeout);
setTimeout(connect, timeout * 1000);
}
function post() {
fetch('/sse/push', {
'method': 'post',
'body': JSON.stringify({
'msg': 'Lalala',
}),
}).then((data) => {
// console.log(data);
});
}
function render() {
eventList.innerText = '';
events.forEach((e) => {
var li = document.createElement('li');
li.innerHTML = 'ping at ' + e.time;
eventList.appendChild(li);
});
}
connect();
</script>
</body>
</html>