-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
38 lines (33 loc) · 1.33 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>SSE Chat</title>
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', function () {
if (typeof (EventSource) !== "undefined") {
const source = new EventSource('sse.php');
source.addEventListener('update', function (event) {
const data = JSON.parse(event.data);
const messageElement = document.createElement('p');
messageElement.textContent = data.created_at + " " + data.name + ":" + data.message;
document.getElementById('messages').appendChild(messageElement);
console.log('id:', data.id);
});
source.addEventListener('close', function (event) {
const messageElement = document.createElement('p');
messageElement.textContent = "Connection closed by server.";
document.getElementById('messages').appendChild(messageElement);
source.close();
});
} else {
alert("Your browser does not support Server-Sent Events.");
}
});
</script>
</head>
<body>
<h1>SSE Chat</h1>
<div id="messages"></div>
</body>
</html>