-
Notifications
You must be signed in to change notification settings - Fork 233
/
Copy pathclient_demo.html
117 lines (116 loc) · 2.94 KB
/
client_demo.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
<!DOCTYPE html>
<html>
<head>
<title>Gameboy.live websocket demo</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.12/dist/vue.js"></script>
</head>
<body>
<div id="app">
<img :src="imageURL" class="centered">
<p class="centered">
<ul>
<li>Arrow keys work as expected</li>
<li><kbd>Z</kbd> → A button</li>
<li><kbd>X</kbd> → B button</li>
<li><kbd>Space</kbd> → Start button</li>
<li><kbd>Shift</kbd> → Select button</li>
</ul>
</p>
</div>
<script>
new Vue({
name: 'App',
data: () => {
return {
wsConn: new WebSocket("ws://127.0.0.1:1989/stream"),
input: "",
imageURL: null,
keys: {
UP: 2,
DOWN: 3,
LEFT: 1,
RIGHT: 0,
A: 4,
B: 5,
START: 7,
SELECT: 6,
}
}
},
methods: {
buttonUp () {
this.wsConn.send(this.keys.UP)
},
buttonDown () {
this.wsConn.send(this.keys.DOWN)
},
buttonLeft () {
this.wsConn.send(this.keys.LEFT)
},
buttonRight () {
this.wsConn.send(this.keys.RIGHT)
},
buttonA () {
this.wsConn.send(this.keys.A)
},
buttonB () {
this.wsConn.send(this.keys.B)
},
buttonStart () {
this.wsConn.send(this.keys.START)
},
buttonSelect () {
this.wsConn.send(this.keys.SELECT)
},
handleKey (event) {
switch (event.code) {
case "ArrowRight":
this.buttonRight();
break;
case "ArrowLeft":
this.buttonLeft();
break;
case "ArrowUp":
this.buttonUp();
break;
case "ArrowDown":
this.buttonDown();
break;
case "KeyZ":
this.buttonA();
break;
case "KeyX":
this.buttonB();
break;
case "Space":
this.buttonStart();
break;
case "ShiftLeft":
case "ShiftRight":
this.buttonSelect();
break;
}
}
},
created () {
this.wsConn.onmessage = (event) => {
if (this.imageURL !== null) {
URL.revokeObjectURL(this.imageURL)
}
this.imageURL = URL.createObjectURL(event.data)
}
},
mounted () {
window.addEventListener("keydown", (event) => this.handleKey(event))
}
}).$mount('#app')
</script>
</body>
</html>
<style>
.centered {
display: block;
margin: 3vh auto;
max-width: 50vw;
}
</style>