-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
151 lines (136 loc) · 4.17 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>countdown</title>
</head>
<style>
.container {
text-align: center;
min-width: 100px;
padding: 3rem;
margin-top: 1rem;
}
.signals .dot {
width: 10px;
height: 10px;
background: #444;
display: inline-block;
border-radius: 50%;
opacity: 0;
animation: blink 0.3s ease-in infinite alternate;
}
.signals .first {
margin-left: 0px;
}
.signals .second {
margin-left: 15px;
animation-delay: 0.1s;
}
.signals .third {
margin-left: 15px;
animation-delay: 0.2s;
}
@keyframes blink {
from {
opacity: 0
}
to {
opacity: 1
}
}
</style>
<body>
<div id="t">
<div class="container">
<div class="signals">
<div class="dot first"></div>
<div class="dot second"></div>
<div class="dot third"></div>
</div>
</div>
</div>
<script>
const url = new URL(window.location.href);
const ws = new WebSocket(`ws://${url.hostname}:8080`);
const t = document.getElementById('t');
ws.onerror = (error) => {
t.innerHTML = `<div class="container"><div class="signals"><div class="dot first"></div><div class="dot second"></div><div class="dot third"></div></div>
</div><div style='color: red; text-align: center;'>Connection Error. Check Console.<div/>`;
console.error('WebSocket error:', error);
};
ws.onopen = () => {
console.clear();
}
ws.onmessage = (event) => {
const recData = JSON.parse(event.data);
const countdown = parseInt(recData.count);
const currentstatus = recData.status;
let btnTxt = '';
switch (currentstatus) {
case 'paused':
btnTxt = 'resume'
document.title = `⏵ ${countdown}`;
break;
case 'running':
btnTxt = 'pause';
document.title = `⏸ ${countdown}`;
break;
default:
btnTxt = 'start';
document.title = 'countdown';
break;
}
if (!t.hasAttribute('set')) {
t.innerHTML = `
<h1 id="count">${countdown >= 0 && currentstatus !== 'stopped' ? countdown : 'Coming Soon!'}</h1>
<button type='button' id="control" onclick="control(this);">${btnTxt.toUpperCase()}</button>
<h3>Lot# <span id="lot" style="color: brown;">${countdown >= 0 && currentstatus !== 'stopped' ? recData.lot || '' : ''}</span></h3>
`;
t.setAttribute('set', 'true')
} else {
const lotEl = document.getElementById('lot'),
countEl = document.getElementById('count'),
controlBtn = document.getElementById('control');
if (countdown >= 0 && currentstatus !== 'stopped') {
countEl.innerText = countdown;
controlBtn.innerText.toUpperCase() == btnTxt.toUpperCase() || (controlBtn.innerText = btnTxt.toUpperCase());
lotEl.innerText == recData.lot || (lotEl.innerText = recData.lot || '');
} else {
countEl.innerText = 'Coming Soon!';
controlBtn.innerText = btnTxt.toUpperCase();
lotEl.innerText = '';
}
}
};
ws.onclose = (event) => {
t.innerHTML = `<div class="container"><div class="signals"><div class="dot first"></div><div class="dot second"></div><div class="dot third"></div></div>
</div><div style='color: orange; text-align: center;'>Connection Closed. Check Console.<div/>`;
console.error('WebSocket connection closed:', event);
}
// controller
function control(btn) {
let status = btn.innerText.toLowerCase();
switch (status) {
case 'start':
ws.send('start');
btn.innerText = 'pause'.toUpperCase();
break;
case 'pause':
ws.send('pause');
btn.innerText = 'resume'.toUpperCase();
break;
case 'resume':
ws.send('resume');
btn.innerText = 'pause'.toUpperCase();
break;
default:
ws.send('stop');
btn.innerText = 'start'.toUpperCase();
break;
}
}
</script>
</body>
</html>