forked from javascript-tutorial/uk.javascript.info
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
50 lines (40 loc) · 1.34 KB
/
index.html
File metadata and controls
50 lines (40 loc) · 1.34 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
<!DOCTYPE html>
<script>
let eventSource;
function start() { // коли кнопку "Пуск" натиснуто
if (!window.EventSource) {
// для IE чи іншого старого браузера
alert("Браузер не підтримує EventSource.");
return;
}
eventSource = new EventSource('digits');
eventSource.onopen = function(e) {
log("Подія: open");
};
eventSource.onerror = function(e) {
log("Подія: error");
if (this.readyState == EventSource.CONNECTING) {
log(`Повторне з’єднання (readyState=${this.readyState})...`);
} else {
log("Сталася помилка.");
}
};
eventSource.addEventListener('bye', function(e) {
log("Подія: bye, дані: " + e.data);
});
eventSource.onmessage = function(e) {
log("Подія: message, дані: " + e.data);
};
}
function stop() { // коли кнопку "Стоп" натиснуто
eventSource.close();
log("eventSource.close()");
}
function log(msg) {
logElem.innerHTML += msg + "<br>";
document.documentElement.scrollTop = 99999999;
}
</script>
<button onclick="start()">Пуск</button> Натісніть "Пуск" для початку.
<div id="logElem" style="margin: 6px 0"></div>
<button onclick="stop()">Стоп</button> "Стоп" для зупинки.