-
Notifications
You must be signed in to change notification settings - Fork 1
/
nodeify.js
123 lines (109 loc) · 3.29 KB
/
nodeify.js
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
(function () {
function addEvent( obj, type, fn ) {
if (obj.addEventListener)
obj.addEventListener( type, fn, false );
else if (obj.attachEvent) {
obj["e"+type+fn] = fn;
obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
obj.attachEvent( "on"+type, obj[type+fn] );
}
}
function removeEvent( obj, type, fn ) {
if (obj.removeEventListener)
obj.removeEventListener( type, fn, false );
else if (obj.detachEvent) {
obj.detachEvent( "on"+type, obj[type+fn] );
obj[type+fn] = null;
obj["e"+type+fn] = null;
}
}
const rate = 20;
var lastSend, lastSentKey, isController = false, eventsAdded = false;
function passKeyPress(key) {
if (lastSend == null) {
socket.send(JSON.stringify(key));
console.log("sending "+ key);
lastSend = +new Date;
lastSentKey = key;
} else if ( (+new Date) - lastSend > rate) {
socket.send(JSON.stringify(key));
console.log("sending "+ key);
lastSend = +new Date;
lastSentKey = key;
} else if ( !isSameKeyPress(key, lastSentKey) ) {
console.log('qued '+ key);
setTimeout(function(){
passKeyPress(key);
}, rate);
} else {
console.log('same key ignored.');
}
}
function isSameKeyPress(key1, key2) {
return key1[0] == key2[0] && key1[1] == key2[1];
}
function keyeventUp(event) {
var event = event || window.event;
console.log("up "+ event.keyCode);
passKeyPress([event.keyCode, false]);
}
function keyeventDown(event) {
var event = event || window.event;
console.log("down "+ event.keyCode);
passKeyPress([event.keyCode, true]);
}
function callback() {
socket = new io.Socket('localhost', {
port: '8080'
});
socket.on('connect', function () {
console.log("I'm connected!", arguments);
});
socket.on('message', function (message) {
try {
var key = JSON.parse(message);
} catch(e) {}
if (key && Array.isArray(key) && window.ASTEROIDSPLAYERS && window.ASTEROIDSPLAYERS.length >0) {
window.ASTEROIDSPLAYERS[0].keysPressed[key[0]] = key[1];
} else {
if (typeof key == "boolean") {
isController = key;
if (!eventsAdded && isController) {
console.log('I am the controller');
addEvent(document, 'keyup', keyeventUp);
addEvent(document, 'keydown', keyeventDown);
}
if (!isController && window.ASTEROIDSPLAYERS ) {
console.log("not the controller");
window.ASTEROIDSPLAYERS[0].shipExit();
}
} else if (key.tel) {
ASTEROIDSPLAYERS[0].shipEnter(key.isRight, key.tel);
}
}
console.log("message: ", message);
});
socket.on('disconnect', function () {
console.log("Disconnected");
if (eventsAdded) {
removeEvent(document, 'keyup', keyeventUp);
removeEvent(document, 'keydown', keyeventDown);
}
socket.connect();
});
socket.connect();
}
var script = document.createElement("script");
script.src = "http://localhost:8080/socket.io/socket.io.js";
if (script.readyState) {
script.onreadystatechange = function () {
if (script.readyState == "loaded" || script.readyState == "complete") {
script.onreadystatechange = null;
callback();
}
};
} else {
script.onload = callback;
}
document.getElementsByTagName("head")[0].appendChild(script);
})();