-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
144 lines (124 loc) · 4.06 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
<html>
<meta http-equiv="Content-Type" content="text/html; Charset=UTF-8" />
<link href='https://fonts.googleapis.com/css?family=Dosis:400,600' rel='stylesheet' type='text/css'>
<head>
<title> synk </title>
<style>
.button {
cursor: pointer;
}
body {
width: 600;
padding-left: 1em;
padding-top: 0.5em;
font-size: 3em;
font-weight: 400;
line-height: 0.8;
background: black;
color: white;
font-family: "Dosis";
}
h1 {
font-size: 2em;
font-weight: 600;
text-align: left;
}
#clickers {
cursor: pointer;
}
</style>
</head>
<body>
<h1>
SCORE <span id="score"></span>
</h1>
<div id="clickers">
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="https://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script>
<script src="https://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/sha3.js"></script>
<script>
/*
* Websocket interfacing *
*/
var subd = 'default'
function pad(n, width, z) {
z = z || '0';
n = n + '';
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}
//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/array/shuffle [v1.0]
function shuffle(o){ //v1.0
for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
};
var last_connection_time = 0,
connecting = false,
host = "ws://synk.herokuapp.com",
conn,
score = 0,
client_id = Math.random().toString(36).slice(2).substring(0,13),
connectWS = function() {
if (window["WebSocket"]) {
conn = new WebSocket(host+"/socket/"+subd)
conn.onopen = function() {
conn.send(client_id)
}
conn.onclose = function (evt) {
console.log("The connection has closed.")
delayedConnect() }
conn.onmessage = function (evt) {
if (evt.data !== ">heartbeat<") {
console.log(evt.data);
code = evt.data.substr(0,4);
$("#clickers").html("");
if (code === "FAIL") {
$("body").css("background", "#a13")
} else if (code === "GOOD"){
$("body").css("background", "#5a5")
score++;
} else if (code === "INIT") {
$("body").css("background", "black")
data = shuffle(evt.data.substr(5).split(" "));
for (var i = 0; i < data.length; i++) {
$("#clickers").append("<span onclick='conn.send(\""+data[i]+"\")'>"+"<img src='http://cdn.loc.gov/service/pnp/prokc/20900/209"+pad(data[i], 2)+"r.jpg' width='200' /></span>")
}
$("#one").html(data[0]);
$("#two").html(data[1]);
$("#three").html(data[2]);
}
$("#score").html(pad(score, 5));
}
}
} else {
console.log("Sadly, your browser does not support WebSockets.")
}
connecting = false
},
delayedConnect = function() {
if ( !connecting ) {
var time = new Date().getTime()
if ( time > last_connection_time ) {
var delay = Math.max(last_connection_time+5000, time) - time
last_connection_time = time + delay
connecting = true
setTimeout( connectWS, delay )
}
}
}
/*
* Message parsing and display *
*/
$(document).ready(connectWS)
var beat = true,
heartbeat = function() {
if (beat && conn.readyState === 1) {
conn.send(">heartbeat<")
}
setTimeout("heartbeat()", 30000)
}
setTimeout("heartbeat()", 60000)
</script>
</body>
</html>