-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsolution.html
More file actions
69 lines (64 loc) · 1.83 KB
/
solution.html
File metadata and controls
69 lines (64 loc) · 1.83 KB
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
<!DOCTYPE html>
<html>
<head>
<title>WebSockets</title>
<script>
window.addEventListener("load", function() {
var token = token;
var task2 = {};
var socket = new WebSocket("ws://localhost:8080");
socket.binaryType = "arraybuffer";
socket.onopen = function() {
socket.send(JSON.stringify({ msg: "challenge_accepted", name: "DaQuirm" }));
};
socket.onmessage = function(evt) {
if (typeof evt.data == "string") {
var msg = JSON.parse(evt.data);
switch (msg.msg) {
case "auth":
token = msg.auth_token;
socket.send(JSON.stringify({ msg: "task_one", auth_token: token }));
break;
case "compute":
var result;
switch (msg.operator) {
case '+':
result = msg.operands[0] + msg.operands[1];
break;
case '-':
result = msg.operands[0] - msg.operands[1];
break;
case '*':
result = msg.operands[0] * msg.operands[1];
break;
}
socket.send(JSON.stringify({ msg: "task_one_result", auth_token: token, result: result }));
break;
case "win":
socket.send(JSON.stringify({ msg: "next", auth_token: token }));
break;
case "binary_sum":
task2 = msg;
break;
}
} else {
var result;
var arrs = {
8: Uint8Array,
16: Uint16Array,
}
var arr = new arrs[task2.bits](evt.data);
var sum = 0;
for (var i = 0; i < arr.length; i++) {
sum += arr[i];
}
result = sum;
socket.send(JSON.stringify({ msg: "task_two_result", auth_token: token, result: result }));
}
}
});
</script>
</head>
<body>
</body>
</html>