-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
97 lines (81 loc) · 3.04 KB
/
server.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
let app = require('express')();
let server = require('http').Server(app);
let io = require('socket.io')(server, {
path: '/socket.io',
});
let users = [];
let connections = [];
let bands = new Map();
const instruments = Object.freeze(["drums", "guitar"]);
app.get('/', function(req, res){
res.sendFile(__dirname + '/index_original.html');
});
app.get('/sounds/:instrument/:sound', function (req, res) {
if ( instruments.indexOf(req.params.instrument) === -1 ) res.sendStatus(403).end();
else res.sendFile(__dirname + `/sounds/${req.params.instrument}/${req.params.sound}`);
});
io.on('connection', function(socket){
connections.push(socket);
console.log('Connected: %s sockets connected', connections.length);
console.log(users);
// New User
socket.on('new user', function (user, callback) {
callback(true);
socket.username = user.username;
socket.instrument = user.instrument;
users.push(socket.username);
// Join Room
// check if room exists && number of users in room
if(io.nsps['/'].adapter.rooms[user.bandname] && io.nsps['/'].adapter.rooms[user.bandname].length > 5) {
// fail
callback(false);
return;
} else {
// join room
socket.join(user.bandname);
// broadcast to room: band name
io.sockets.in(user.bandname).emit('connectToRoom', user.bandname);
// only add band the first time
if ( bands.get(user.bandname) === undefined )
bands.set(user.bandname, 1);
else
bands.set(user.bandname, bands.get(user.bandname) + 1);
updateBands();
}
updateUsernames();
});
// Key Press Received
socket.on('new key press', function (keyEventData) {
socket.broadcast.to(keyEventData.bandname).emit('get key press', { instrument: keyEventData.instrument, key: keyEventData.keyCode });
});
// Get List of All Users and Bands
socket.on('get active users', function() { updateUsernames();updateBands(); console.log("Rooms", Object.keys(io.nsps['/'].adapter.rooms)); });
function updateBands() {
let bandObj = {
count: Array.from(bands.values()),
bands: Array.from(bands.keys())
};
io.sockets.emit('get bands', JSON.stringify(bandObj));
}
function updateUsernames() {
io.sockets.emit('get users', users);
}
// On Before Disconnect
socket.on('disconnecting', function () {
let currentRooms = Object.keys(this.rooms);
currentRooms.forEach(function (currentRoom) {
let bandCount = bands.get(currentRoom);
// if room is a band
if (bandCount !== undefined) {
if (bandCount !== 1)
bands.set(currentRoom, bands.get(currentRoom) - 1);
else
bands.delete(currentRoom);
}
});
updateBands();
});
});
server.listen(3000, function(){
console.log('listening on *:3000');
});