This repository has been archived by the owner on Sep 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
56 lines (53 loc) · 1.67 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
53
54
55
56
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="app.css" class="style" />
<title>Stream</title>
</head>
<body>
<div class="container">
<div id="renting" class="channel">
<h1>renting</h1>
<div class="subscribers">0</div>
<button onclick="subscribe('renting')">Subscribe</button>
</div>
<div id="aviation" class="channel">
<h1>aviation</h1>
<div class="subscribers">0</div>
<button onclick="subscribe('aviation')">Subscribe</button>
</div>
<div id="shipping" class="channel">
<h1>shipping</h1>
<div class="subscribers">0</div>
<button onclick="subscribe('shipping')">Subscribe</button>
</div>
</div>
<script>
async function subscribe(companyId) {
const connection = await fetch(`subscribe/${companyId}`);
const response = await connection.json();
console.log(response);
}
const eventSource = new EventSource("/sse");
eventSource.addEventListener("message", (e) => {
try {
if (e.lastEventId === "-1") {
console.log("last event");
eventSource.close();
} else {
console.log(e.data);
const companies = JSON.parse(e.data);
for (companyId in companies) {
document.querySelector(`#${companyId} .subscribers`).innerHTML =
companies[companyId].subscribers;
}
}
} catch (error) {
console.error(error);
}
});
</script>
</body>
</html>