-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathdemo.html
80 lines (66 loc) · 2.11 KB
/
demo.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
71
72
73
74
75
76
77
78
79
80
<!doctype html>
<html>
<head>
<title>sse.js test</title>
<style type="text/css">
body { width: 60%; margin: 2em auto; font-family: monospace; }
#content { display: flex; flex-direction: column-reverse; }
#content div { border: 1px #eee solid; background: #f5f5f5; padding: 1em; margin: 1em 0; }
#content span { display: block; text-align: right; color: #ccc; font-size: small; }
#content pre { margin: 0; }
</style>
</head>
<body>
<header>
<h1>sse.js streaming demo</h1>
</header>
<main>
<section>
<label for="url">Enter a SSE source URL:</label>
<input type="url" id="url" size="40" required />
<input type="button" id="go" value="Start" />
</section>
<section id="content">
</section>
</main>
<script type="module">
import { SSE } from './lib/sse.js';
var url = document.getElementById('url');
var button = document.getElementById('go');
var content = document.getElementById('content');
var source;
function show(e) {
if (typeof e.data === 'string' && e.data.startsWith('{')) {
e.data = JSON.parse(e.data);
}
if (typeof e.data.time === 'number') {
e.data.time = new Date(e.data.time * 1000);
}
var el = document.createElement('div');
var span = document.createElement('span');
span.appendChild(document.createTextNode(new Date().toString()));
el.appendChild(span);
var pre = document.createElement('pre');
pre.appendChild(document.createTextNode(JSON.stringify(e.data, null, 2)));
el.appendChild(pre);
content.append(el);
}
function start() {
content.innerHTML = '';
source = new SSE(url.value, {start: false, debug: true});
source.addEventListener('message', show);
source.stream();
button.value = 'Stop';
button.onclick = reset;
}
function reset() {
if (source) {
source.close();
}
button.value = 'Start';
button.onclick = start;
}
reset();
</script>
</body>
</html>