-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
52 lines (45 loc) · 1.37 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<!DOCTYPE html>
<html>
<head>
<style>
#status {
color: blue;
}
.message {
border: 1px solid black;
padding: 10px;
margin: 10px 0;
}
</style>
</head>
<body>
<h1>Push Stream Event Listener</h1>
<h2 id="status">Connecting...</h2>
<div id="container"></div>
<script>
var source = new EventSource('http://localhost:8080/sse');
var statusElement = document.getElementById('status');
var containerElement = document.getElementById('container');
source.onopen = function(event) {
statusElement.textContent = 'Connection was established';
statusElement.style.color = 'green';
};
source.onerror = function(event) {
statusElement.textContent = 'An error has occurred';
statusElement.style.color = 'red';
};
source.onmessage = function(event) {
var messageElement = document.createElement('div');
messageElement.className = 'message';
messageElement.textContent = 'New message received: ' + event.data;
containerElement.appendChild(messageElement);
};
source.addEventListener('user-connected', function(event) {
var messageElement = document.createElement('div');
messageElement.className = 'message';
messageElement.textContent = 'User connected: ' + event.data;
containerElement.appendChild(messageElement);
});
</script>
</body>
</html>