-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample.html
62 lines (49 loc) · 2.21 KB
/
sample.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>sample</title>
</head>
<body>
<h3>Received messages: </h3>
<ul id="msg-recv">
</ul>
<br>
<p>Connection state: <span id="connectionState">disconnected</span></p>
<script type="application/javascript">
window.onload = function() {
const messageListElement = document.getElementById("msg-recv");
const connectionStateElement = document.getElementById("connectionState");
const serverNotifications = new EventSource("http://localhost:3000/push/register");
serverNotifications.onopen = function() {
connectionStateElement.textContent = "connected";
};
serverNotifications.addEventListener("new-item", function(message) {
const messageContainer = document.createElement("li");
messageContainer.textContent = "NEW ITEM message: " + message.data;
messageListElement.appendChild(messageContainer);
if (Notification.permission === "granted") {
new Notification("New message!", {body: message.data});
}
});
serverNotifications.addEventListener("update-item", function(message) {
const messageContainer = document.createElement("li");
messageContainer.textContent = "ITEM UPDATE message: " + message.data;
messageListElement.appendChild(messageContainer);
if (Notification.permission === "granted") {
new Notification("New update!", {body: message.data});
}
});
serverNotifications.addEventListener("remove-item", function(message) {
const messageContainer = document.createElement("li");
messageContainer.textContent = "ITEM DELETED message: " + message.data;
messageListElement.appendChild(messageContainer);
if (Notification.permission === "granted") {
new Notification("New deletion!", {body: message.data});
}
});
Notification.requestPermission();
}
</script>
</body>
</html>