-
Notifications
You must be signed in to change notification settings - Fork 2
/
index_original.html
183 lines (154 loc) · 6.77 KB
/
index_original.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Jam Space</title>
</head>
<body>
<form action="/" id="userForm" onsubmit="createUser(event)">
<label for="username">Username:</label>
<input id="username" type="text">
<br>
<label for="join-band-radio">Join</label><input type="radio" name="band-select" id="join-band-radio" checked>
<label for="create-band-radio">Create</label><input type="radio" name="band-select" id="create-band-radio">
<br>
<label for="join-band">Join Band:</label>
<select id="join-band">
<option value="0">--No Bands Available--</option>
</select>
<br>
<label for="create-band">Create Band:</label>
<input id="create-band" type="text" disabled>
<br>
<label for="instrument">Instrument: </label>
<select id="instrument">
<option value="drum">drum</option>
<option value="guitar">guitar</option>
<option value="other">other</option>
</select>
<br>
<input type="submit" value="Join">
</form>
<hr>
<h4>Connected Users</h4>
<ul id="users">
<li style="list-style-type:none;float:left;margin-top:-1rem;margin-left:-2rem;padding-left:.5rem">0 users connected</li>
</ul>
<audio data-instrument="drum" data-default-key="65" data-custom-key="65" src="/sounds/drums/snare.wav"></audio>
<audio data-instrument="guitar" data-default-key="65" data-custom-key="65" src="/sounds/guitar/hihat.wav"></audio>
<audio data-instrument="other" data-default-key="65" data-custom-key="65" src="/sounds/other/file.wav"></audio>
<script src="/socket.io/socket.io.js"></script>
<script>
let socket = io();
let userForm = document.getElementById('userForm');
let username = document.getElementById('username');
let instrument = document.getElementById('instrument');
let bandSelectors = document.querySelectorAll('input[name="band-select"]');
let bandSelected = document.querySelector('input[name="band-select"][checked]');
let joinBandInput = document.getElementById('join-band');
let createBandInput = document.getElementById('create-band');
let users = document.getElementById('users');
let drums = [...document.querySelectorAll("audio[data-instrument=drum]")];
let guitars = [...document.querySelectorAll("audio[data-instrument=guitar]")];
let instrumentType = '';
let bandName = '';
socket.emit('get active users');
function createUser(e){
e.preventDefault();
if (username.value !== '') {
bandName = (bandSelected.id === 'join-band-radio') ? joinBandInput[joinBandInput.selectedIndex].value : createBandInput.value;
let user = {
username: username.value,
bandname: bandName,
instrument: instrument[instrument.selectedIndex].value
};
socket.emit('new user', user, function (isSuccess) {
console.log(isSuccess);
if (isSuccess) {
userForm.style.opacity = 0;
username.value = '';
instrumentType = instrument[instrument.selectedIndex].value;
}
});
}
}
window.addEventListener("keyup", function (e) {
let key = e.keyCode;
// alhpanum
if ( (key >= 65 && key <= 90) || (key >= 48 && key <= 57))
{
let keyEvent = {
keyCode: key,
instrument: instrumentType,
bandname: bandName
};
if (instrumentType === "drum") {
let sound = drums.filter( el => el.dataset.customKey === key.toString())[0];
sound.currentTime = 0;
sound.play().then();
}
if (instrumentType === "guitar") {
let sound = guitars.filter( el => el.dataset.customKey === key.toString())[0];
sound.currentTime = 0;
sound.play().then();
}
socket.emit('new key press', keyEvent);
}
});
bandSelectors.forEach(function (radioBtn) {
radioBtn.addEventListener('change', function (e) {
bandSelected = e.target;
if (e.target.id === 'join-band-radio') {
joinBandInput.disabled = false;
createBandInput.disabled = true;
} else {
joinBandInput.disabled = true;
createBandInput.disabled = false;
createBandInput.focus();
}
});
});
socket.on('get users', function (usersList) {
console.log("userList", usersList);
if (usersList.length !== 0) {
let s = '';
for (i = 0; i < usersList.length; i++) {
s += `<li>${usersList[i]}</li>`;
}
users.innerHTML = s;
}
});
socket.on('get bands', function (bandsObj) {
let bandsMap = JSON.parse(bandsObj);
console.log("bandsMap", bandsMap);
if (bandsMap.bands.length !== 0){
let s = '';
bandsMap.bands.forEach(function(value, index){
s += `<option value="${value}">${value}: ${bandsMap.count[index]}</option>`;
});
joinBandInput.innerHTML = s;
}
});
socket.on('connectToRoom',function(roomNo) {
document.title = "Jam Space "+roomNo;
});
socket.on('get key press', function(keyEventData) {
console.log("received: ", keyEventData);
if (keyEventData.instrument === "drum") {
let sound = drums.filter( el => el.dataset.defaultKey === keyEventData.key.toString())[0];
console.log(sound);
sound.currentTime = 0;
sound.play().then();
}
if (keyEventData.instrument === "guitar") {
let sound = guitars.filter( el => el.dataset.defaultKey === keyEventData.key.toString())[0];
sound.currentTime = 0;
sound.play().then();
}
});
</script>
</body>
</html>