-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathdemo.html
More file actions
257 lines (232 loc) · 6.82 KB
/
demo.html
File metadata and controls
257 lines (232 loc) · 6.82 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<!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;
}
.error {
background-color: #fee !important;
color: #900;
}
.info {
background-color: #eef !important;
color: #009;
}
.controls {
margin: 1em 0;
display: flex;
gap: 1em;
align-items: center;
}
.controls label {
display: flex;
align-items: center;
gap: 0.5em;
}
.controls input[type="number"] {
width: 6em;
}
.controls-group {
border: 1px solid #eee;
padding: 1em;
margin: 1em 0;
border-radius: 4px;
}
.controls-group h3 {
margin: 0 0 1em 0;
color: #666;
font-size: 0.9em;
}
</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" />
<div class="controls-group">
<h3>Connection Settings</h3>
<div class="controls">
<label>
<input type="checkbox" id="auto-reconnect" checked />
Auto-reconnect
</label>
<label>
<input
type="number"
id="reconnect-delay"
value="3000"
min="500"
step="500"
/>
ms delay
</label>
</div>
<div class="controls">
<label>
<input type="checkbox" id="use-last-event-id" />
Use Last-Event-ID header
</label>
</div>
<div class="controls">
<label>
<input
type="number"
id="max-retries"
value=""
min="1"
placeholder="∞"
/>
max retries (empty = unlimited)
</label>
</div>
</div>
</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 autoReconnect = document.getElementById("auto-reconnect");
var reconnectDelay = document.getElementById("reconnect-delay");
var useLastEventId = document.getElementById("use-last-event-id");
var maxRetries = document.getElementById("max-retries");
var source;
function addMessage(data, type = "") {
var el = document.createElement("div");
if (type) {
el.className = type;
}
var span = document.createElement("span");
span.appendChild(document.createTextNode(new Date().toString()));
el.appendChild(span);
var pre = document.createElement("pre");
pre.appendChild(
document.createTextNode(
typeof data === "object"
? JSON.stringify(data, null, 2)
: String(data)
)
);
el.appendChild(pre);
content.append(el);
}
function show(e) {
console.log("Received SSE message:", e);
let data = e.data;
try {
if (
typeof data === "string" &&
(data.startsWith("{") || data.startsWith("["))
) {
data = JSON.parse(data);
if (data.time && typeof data.time === "number") {
data.time = new Date(data.time * 1000);
}
}
} catch (err) {
console.warn("Failed to parse message data as JSON:", err);
}
addMessage(data);
}
function start() {
if (!url.value) {
addMessage("Please enter a valid SSE URL", "error");
return;
}
content.innerHTML = "";
addMessage("Connecting to " + url.value, "info");
const options = {
start: false,
debug: true,
autoReconnect: autoReconnect.checked,
reconnectDelay: parseInt(reconnectDelay.value, 10),
useLastEventId: useLastEventId.checked,
maxRetries: maxRetries.value ? parseInt(maxRetries.value, 10) : null,
};
if (options.autoReconnect) {
const retryMsg = options.maxRetries
? `Auto-reconnect enabled (${options.reconnectDelay}ms delay, max ${options.maxRetries} retries)`
: `Auto-reconnect enabled (${options.reconnectDelay}ms delay, unlimited retries)`;
addMessage(retryMsg, "info");
}
source = new SSE(url.value, options);
source.addEventListener("message", show);
source.addEventListener("open", function (e) {
addMessage("Connection opened", "info");
});
source.addEventListener("error", (e) => {
const msg = e.data
? `Error: ${e.data}`
: "Error: Unknown error occurred";
addMessage(msg, "error");
const nextAttempt = source.retryCount + 1;
if (source.maxRetries && nextAttempt > source.maxRetries) {
// Max retries reached, reset button
button.value = "Start";
button.onclick = start;
addMessage(
`Max retries (${source.maxRetries}) reached, connection permanently closed`,
"info"
);
} else if (!source.autoReconnect) {
// No auto-reconnect, reset button
button.value = "Start";
button.onclick = start;
} else {
// Still retrying
addMessage(
`Will attempt to reconnect in ${
options.reconnectDelay
}ms (attempt ${nextAttempt}${
source.maxRetries ? "/" + source.maxRetries : ""
})`,
"info"
);
}
});
source.stream();
button.value = "Stop";
button.onclick = reset;
}
function reset() {
if (source) {
source.close();
addMessage("Connection closed", "info");
}
button.value = "Start";
button.onclick = start;
}
reset();
</script>
</body>
</html>