-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpracticehtml.html
More file actions
83 lines (67 loc) · 1.76 KB
/
practicehtml.html
File metadata and controls
83 lines (67 loc) · 1.76 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<html>
<script>
var isDown = false;
var isUp = false;
var isLeft = false;
var isRight = false;
var threeList = null;
var socket = new WebSocket("ws://10.9.48.8:9876");
var socketMap = new WebSocket("ws:localhost:1234");
// send message when the key is pressed
document.addEventListener("keydown", move, false);
function move(e) {
var keyCode = e.keyCode;
if (!isDown && keyCode == 40){
socket.send("down");
isDown = true;
}
else if (!isLeft && keyCode == 37) {
socket.send("left");
isLeft = true;
}
else if (!isUp && keyCode == 38) {
socket.send("up");
isUp = true;
}
else if (!isRight && keyCode == 39){
socket.send("right");
isRight = true;
}
}
// when you let go of the key
document.addEventListener("keyup", halt, false);
function halt(e) {
console.log(e.keyCode);
var keyCode = e.keyCode;
if (keyCode == 40){
socket.send("stopDown");
isDown = false;
}
else if (keyCode == 38) {
socket.send("stopUp");
isUp = false;
}
else if (keyCode == 37 ) {
socket.send("stopLeft");
isLeft = false;
}
else if (keyCode == 39) {
socket.send("stopRight");
isRight = false;
}
}
// receiving message
socket.onmessage = function(event) {
var u, x;
u = URL.createObjectURL(event.data);
x = new XMLHttpRequest();
x.open('GET', u, false); // although sync, you're not fetching over internet
x.send();
URL.revokeObjectURL(u);
console.log(JSON.parse(x.responseText));
// console.log(JSON.parse(event.data));
threeList = x.responseText;
socketMap.send(threeList);
}
</script>
</html>