-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient-ui.php
More file actions
134 lines (111 loc) · 3.46 KB
/
client-ui.php
File metadata and controls
134 lines (111 loc) · 3.46 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
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
<html>
<head>
<title>Cloud Car</title>
<script src="jquery.js"></script>
</head>
<body>
<div style="float:left; width:70%; background:#ccc; height:600px;">
<iframe src="http://192.168.1.124:3000/" style="width:100%; height:100%;"></iframe>
</div>
<fieldset>
<legend style="float:left; width:30%;">
</legend>
<div id="gamepad">
<h1>Nessun Controller connesso</h1>
</div>
</fieldset>
<div id="socketconsole" style="border:1px solid #ccc; width:30%; float:left; height:200px">
</div>
<button type="button" onClick="testsocket();">Test Socket</button>
</body>
</html>
<script type="text/javascript">
document.onkeydown = checkKey;
function checkKey(e) {
e = e || window.event;
if (e.keyCode == '38') {
// up arrow
socket.send("7");
}
else if (e.keyCode == '40') {
// down arrow
socket.send("6");
}
else if (e.keyCode == '37') {
// left arrow
}
else if (e.keyCode == '39') {
// right arrow
}
}
// global gamepad object
let gamepadIndex;
window.addEventListener('gamepadconnected', (event) => {
gamepadIndex = event.gamepad.index;
});
setInterval(() => {
if(gamepadIndex !== undefined) {
// a gamepad is connected and has an index
const myGamepad = navigator.getGamepads()[gamepadIndex];
//document.body.innerHTML = ""; // reset page
$("#gamepad").html("");
myGamepad.buttons.map(e => e.pressed).forEach((isPressed, buttonIndex) => {
if(isPressed) {
// button is pressed; indicate this on the page
//document.body.innerHTML += `<h1>Button ${buttonIndex} is pressed</h1>`;
$("#gamepad").html( `<h1>Button ${buttonIndex} is pressed</h1>`);
//sendbutton(buttonIndex);
socket.send(buttonIndex);
console(buttonIndex);
}
})
}
}, 100) // print buttons that are pressed 10 times per second
</script>
<script>
let socket = new WebSocket("ws://192.168.1.124:8080");
console.log(socket.readyState);
socket.onmessage = function(e){ console.log(e.data); };
console.log(socket.readyState);
socket.onopen = () => conn.send('hello');
socket.onmessage = function(event) {
//lert(`[message] Ricezione dati dal server: ${event.data}`);
$("#socketconsole").append("Ricezione dati dal server: "+event.data+"<br>");
console.log("Ricezione dati dal server: "+event.data);
};
socket.onclose = function(event) {
if (event.wasClean) {
//alert(`[close] Connessione chiusa con successo, code=${event.code} reason=${event.reason}`);
$("#socketconsole").append("Connessione chiusa: "+event.code+"<br>")
console.log(socket.readyState);
} else {
// e.g. processo del server terminato o connessione già
// in questo caso event.code è 1006
// alert('[close] Connection morta.');
$("#socketconsole").append("Server Socket offline<br>")
console.log(socket.readyState);
}
};
socket.onopen = function(e) {
alert("[open] Connessione stabilita");
$("#socketconsole").append("Connessione stabilita<br>");
// alert("Invio al server");
$("#socketconsole").append("Invio al server i dati<br>");
$("#socketconsole").append(socket.readyState);
socket.send("Sono connesso baby");
//socket.send("Il mio nome è John");
};
socket.onerror = function(error) {
//alert(`[error] ${error.message}`);
$("#socketconsole").append("Errore "+error.message+"<br>");
console.log(socket.readyState);
};
function sendbutton(button) {
console.log(socket.readyState);
socket.send(button);
}
function testsocket() {
console.log(socket.readyState);
socket.send("Sono il dato inviato via socket");
}
</script>