-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudience.html
288 lines (271 loc) · 8.79 KB
/
audience.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<!doctype html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<meta charset="utf-8">
<title>Remote Applause</title>
<link href="https://fonts.googleapis.com/css2?family=Fira+Sans:wght@600&family=Merriweather&display=swap" rel="stylesheet">
<style>
html, body { margin: 0; padding: 0; }
#nojs-warning { opacity: 0; color: red; animation: fadein 250ms ease-out; animation-delay: 3000ms; }
body.js #nojs-warning { animation: none; display: none; }
@keyframes fadein { from { opacity: 0; display: block; } to { opacity: 1; } }
body {
background: url(stage.jpg) center center black;
background-size: cover;
background-repeat: no-repeat;
height: 100vh;
font-family: Merriweather, serif;
color: #f1f2f3;
}
h1 {
font-family: "Fira Sans";
text-align: center;
margin: 0;
padding: 1em;
text-shadow: 0 0 4px black;
}
#messages {
color: #ee9;
margin: 1em;
}
#buttons {
position: absolute;
bottom: 0;
width: 100%;
left: 0;
padding: 1vh;
box-sizing: border-box;
}
#buttons button {
width: 80%;
margin-left: 10%;
margin-top: 1vh;
border-radius: 9px;
border: 5px solid #eaa;
background-color: rgba(0, 0, 0, 0.7);
font-size: 6vh;
background-repeat: no-repeat;
background-position: center center;
touch-action: manipulation;
}
#btnClap {
text-indent: -1000em;
background-image: url("claps/1F44F.svg");
}
#btnLaugh {
text-indent: -1000em;
background-image: url("1F602.svg");
}
@media screen and (min-width: 600px) {
#buttons {
display: flex;
flex-direction: row;
justify-content: space-around;
}
#buttons button {
width: 40%;
margin: 0;
}
}
div.indicator {
position: absolute;
bottom: 50px;
transition: all 3s ease-out;
transform: translateY(0);
opacity: 1;
pointer-events: none;
}
div.indicator img {
position: absolute;
top: 0;
animation: wiggle 700ms linear infinite;
width: 3vw;
min-width: 50px;
height: 3vw;
min-height: 50px;
}
@keyframes wiggle {
0% { transform: translateX(0); }
20% { transform: translateX(-13.5%); }
25% { transform: translateX(-15%); }
30% { transform: translateX(-13.5%); }
50% { transform: translateX(0); }
70% { transform: translateX(13.5%); }
75% { transform: translateX(15%); }
80% { transform: translateX(13.5%); }
100% { transform: translateX(0); }
}
</style>
</head>
<body>
<h1>Remote Applause <output></output></h1>
<div id="messages">
loading...
<div id="nojs-warning">Unfortunately, this service requires JavaScript, which
does not seem to be available. If you have it turned off then turning it
on may help; if not, then refreshing this page may help. Sorry about that.</div>
</div>
<div id="buttons"></div>
<script src="https://unpkg.com/peerjs@1.0.0/dist/peerjs.min.js"></script>
<script>
console.clear();
var CLAPS = [
"claps/1F44F-1F3FB.svg",
"claps/1F44F-1F3FD.svg",
"claps/1F44F-1F3FF.svg",
"claps/1F44F-1F3FC.svg",
"claps/1F44F-1F3FE.svg"
];
function hasher(inp) { var hash = 0; if (inp.length == 0) { return hash; }
for (var i = 0; i < inp.length; i++) { var char = inp.charCodeAt(i);
hash = ((hash<<5)-hash)+char; hash = hash & hash; } return hash; }
function showItem(url) {
var img = document.createElement("img");
var div = document.createElement("div");
div.className = "indicator";
img.style.left = ((Math.random() * 80) + 10) + "vw";
div.addEventListener("transitionend", function() {
//console.log("removing", div);
div.remove();
})
div.appendChild(img);
document.body.appendChild(div);
var up = (Math.random() * 50) + 25;
//console.log("creating", img);
img.src = url;
img.offsetWidth; // wait until layout is done, otherwise the transition gets ignored
div.style.transform = "translateY(-" + up + "vh)";
div.style.opacity = 0;
}
function showClap() {
showItem(CLAPS[Math.floor(Math.random() * CLAPS.length)]);
}
function showLaugh() {
showItem("1F602.svg");
}
var conf = location.search;
if (conf == "") {
location.href = "index.html";
} else {
var usp = new URLSearchParams(window.location.search);
conf = usp.get('c');
}
document.querySelector("output").textContent = "for " + conf;
var confid = "rapp" + Math.abs(hasher(conf));
var retryTimer = 1;
var messages = document.getElementById("messages");
var rti = null;
var checkConnectionI;
var latest_error = "";
function connect() {
clearInterval(rti);
function retry() {
if (rti !== null) {
console.log("already retrying, so not retrying again", rti);
return;
}
retryTimer *= 2;
if (retryTimer > 30) retryTimer = 30;
messages.innerHTML = "Connection problems" + latest_error + ". Reconnecting in <span>" + retryTimer + "</span> seconds...";
var buttons = document.getElementById("buttons");
buttons.innerHTML = "";
var rt = retryTimer;
rti = setInterval(function() {
console.log("waiting to actually do a retry")
rt -= 1;
if (rt == 0) {
clearInterval(rti);
console.log("ending retry with rti", rti);
rti = null;
connect();
return;
}
var spans = messages.getElementsByTagName("span");
if (spans.length > 0) {
spans[0].textContent = rt;
} else {
// our span has disappeared; someone's overwritten the messages div
}
}, 1000);
console.log("kicking off retry with rti", rti, "and times", retryTimer);
}
var peer = new Peer();
peer.on('open', function(id) {
console.log("audience peer connected to server with ID", id);
messages.textContent = "Waiting for presenter...";
retryTimer = 1;
});
peer.on('error', function(err) {
console.log("audience peer error connecting to server", err.type, err);
peer.destroy();
latest_error = " (with the server)"
retry();
});
peer.on('disconnected', function() {
console.log("audience peer disconnected from server");
latest_error = " (we lost the server connection)"
peer.destroy();
retry();
});
peer.on('close', function() {
console.log("audience: I am destroyed");
latest_error = " (everything collapsed, sorry)"
retry();
});
var conn = peer.connect(confid);
// Firefox gets conn.on("close") events. Chrome doesn't. So we check the state
// frequently ourselves and reconnect if the peerConnection has gone away.
checkConnectionI = setInterval(function() {
if (conn.peerConnection && conn.peerConnection.connectionState !== undefined &&
conn.peerConnection.connectionState != "connected" && conn.peerConnection.connectionState != "new" &&
conn.peerConnection.connectionState != "connecting") {
console.log("Connection seems to have dropped (state is", conn.peerConnection.connectionState, ". Forcing reconnection!");
clearInterval(checkConnectionI);
latest_error = " (with the network)"
retry();
}
}, 3000);
conn.on('open', function() {
console.log("audience connected to stage");
});
conn.on('close', function() {
console.log("audience disconnected from stage; retrying");
latest_error = " (we can't see the presenter)"
retry();
});
conn.on('error', function(err) {
console.log("audience to stage connection error", err);
});
conn.on('data', function(data) {
console.log("audience from stage, data:", data);
if (data == "readyForClaps") {
messages.textContent = "";
var buttons = document.getElementById("buttons");
buttons.innerHTML = "";
var btnClap = document.createElement("button");
btnClap.id = "btnClap";
btnClap.textContent = "clap";
btnClap.onclick = function() { conn.send("clap"); }
buttons.appendChild(btnClap);
var btnLaugh = document.createElement("button");
btnLaugh.id = "btnLaugh";
btnLaugh.textContent = "laugh";
btnLaugh.onclick = function() { conn.send("laugh"); }
buttons.appendChild(btnLaugh);
} else if (data == "pauseClaps") {
var buttons = document.getElementById("buttons");
buttons.innerHTML = "";
messages.textContent = "waiting for presenter...";
} else if (data == "clap") {
showClap();
} else if (data == "laugh") {
showLaugh();
}
});
}
document.body.className = "js";
connect();
</script>
</body>
</html>