Skip to content

Commit be06821

Browse files
author
Glitch (coro-per)
committedFeb 18, 2020
fix eventsource being broken & make header more fancy
1 parent 6cf5e59 commit be06821

File tree

5 files changed

+49
-13
lines changed

5 files changed

+49
-13
lines changed
 

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@
2727
"glitch",
2828
"express"
2929
]
30-
}
30+
}

‎public/client.js

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
const $ = document.querySelector.bind(document);
22
const $$ = document.querySelectorAll.bind(document);
33

4+
let { applyData } = window;
5+
6+
/* EVENTSOURCE */
47
let evtSource = (window.evtSource = {});
58
const join = force => {
69
force && evtSource.close();
710
evtSource = new EventSource(`./events`);
11+
812
evtSource.addEventListener(
9-
"data",
13+
"event",
1014
evt => {
1115
const payload = JSON.parse(evt.data);
12-
const data = payload.data;
16+
const DATA = payload.data;
17+
1318
switch (payload.type) {
1419
case "infected-update":
1520
{
16-
console.log(data);
21+
applyData(DATA);
1722
}
1823
break;
1924
}
@@ -27,7 +32,7 @@ fetch("/init")
2732
.then(res => res.json())
2833
.then(json => applyData(json));
2934

30-
const applyData = count => {
35+
applyData = count => {
3136
/////// ppl count by https://world-statistics.org/
3237
var d1 = new Date().getTime();
3338
var d0 = new Date(2019, 6, 1, 0, 0, 0, 0).getTime();
@@ -52,7 +57,9 @@ const updateLocal = setInterval(() => applyData(window.count), 1399);
5257

5358
const rejoiner = setInterval(() => {
5459
const rejoin = () => join(true);
55-
evtSource.readyState !== EventSource.OPEN
60+
evtSource.readyState === EventSource.CLOSED
5661
? [console.warn("No connection, rejoining..."), rejoin()]
57-
: console.debug("Connection ok :)");
62+
: console.debug(
63+
`Connection ${evtSource.readyState === 0 ? "waiting..." : "ok :)"}`
64+
);
5865
}, 9999);

‎public/style.css

+10-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ body {
3636
left: 0;
3737
width: 100%;
3838
height: 100%;
39-
color: hsla(0, 100%, 100%, 1);
39+
color: white;
4040
flex-direction: column;
4141
position: absolute;
4242
display: flex;
@@ -47,6 +47,7 @@ body {
4747
mix-blend-mode: difference;
4848
text-transform: uppercase;
4949
font-family: virus;
50+
text-align: center;
5051
}
5152

5253
svg {
@@ -55,10 +56,17 @@ svg {
5556

5657
.small {
5758
margin-top: 1rem;
58-
color: lightgray;
59+
color: dimgray;
5960
font-size: 50%;
6061
}
6162

63+
#header {
64+
font-size: 25%;
65+
color: lightgray;
66+
text-decoration: underline;
67+
margin-bottom: 2vh;
68+
}
69+
6270
footer {
6371
position: fixed !important;
6472
width: 100%;

‎server.js

+19-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ const //imports
99
//config emitter
1010
const emitter = new EventEmitter();
1111

12-
app.get(`/events`, (req, res) => {
12+
app.get("/events", (req, res) => {
13+
console.log("New client");
1314
res.writeHead(200, {
1415
"Content-Type": "text/event-stream",
1516
"Cache-Control": "no-cache",
@@ -32,6 +33,7 @@ app.get(`/events`, (req, res) => {
3233

3334
// Clear heartbeat and listener
3435
req.on("close", () => {
36+
console.log("Client disconnected");
3537
clearInterval(hbt);
3638
emitter.removeListener("event", onEvent);
3739
});
@@ -72,12 +74,26 @@ const getCount = () => {
7274
});
7375

7476
emitter.emit("event", {
75-
type: " infected-update",
77+
type: "infected-update",
7678
data: count
7779
});
7880
};
7981

80-
getCount();
82+
/*
83+
//debug, delete me maybe
84+
let i = 0;
85+
setInterval(() => {
86+
count.dead = i;
87+
i++;
88+
emitter.emit("event", {
89+
type: "infected-update",
90+
data: count
91+
});
92+
}, 999);
93+
*/
94+
95+
//first init of data
96+
setTimeout(getCount);
8197

8298
//interval to fetch infected-data from host
8399
const intervalMinutes = 30;

‎views/index.html

+6-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@
2323
</head>
2424
<body>
2525
<div class="content-canvas"><canvas id="canvas"></canvas></div>
26-
<div id="infected" class="text">Loading...</div>
26+
<div class="text">
27+
<span id="header"
28+
>Percentage of earth habitants currently infected with the COVID-19-Coronavirus:
29+
</span>
30+
<span id="infected">Loading...</span>
31+
</div>
2732
<svg id="weirdFilter">
2833
<filter id="svgFilter">
2934
<feturbulence

0 commit comments

Comments
 (0)
Please sign in to comment.