-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
348 lines (317 loc) · 13.2 KB
/
server.js
File metadata and controls
348 lines (317 loc) · 13.2 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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
(function () {
'use strict';
// Dependencies
const restify = require('restify');
const socketio = require('socket.io');
const redis = require('redis');
const Promise = require('bluebird');
const socketIORedis = require('socket.io-redis');
// Setup Redis client with promises
Promise.promisifyAll(redis.RedisClient.prototype);
Promise.promisifyAll(redis.Multi.prototype);
// Configuration
const port = 8080;
const burst = 100;
const throttleRate = 50;
var server = restify.createServer();
var io = socketio.listen(server);
var redisClient = redis.createClient({
host: process.env.REDIS_HOST,
port: process.env.REDIS_PORT
});
io.adapter(socketIORedis({host: process.env.REDIS_HOST, port: process.env.REDIS_PORT}));
server.use(restify.CORS());
server.use(restify.gzipResponse());
server.use(restify.bodyParser());
server.use(restify.throttle({
burst: burst,
rate: throttleRate,
ip: true
}));
server.use(restify.conditionalRequest());
// Health Check
server.get('/', function (req, res) {
res.status(200);
res.send({
message: 'Oh hai there!'
});
});
var roomNamespaces = {};
var defaultInstruments = {
drums: false,
bass: false,
lead: false,
rhythm: false
};
console.log('Setting up current rooms');
redisClient
.getAsync('rooms')
.then(JSON.parse)
.then(function (rooms) {
rooms = rooms || [];
rooms.forEach(function (room) {
console.log('Setting up ' + room + ' from Redis');
setupRoom(room);
});
});
redisClient.on('error', function (err) {
console.log('Redis Error ' + err);
});
server.get('/flush', function (req, res) {
console.log('Flushing Current State');
redisClient
.flushallAsync()
.then(function () {
res.status(204);
res.end();
});
Object.keys(io.nsps).forEach(function (namespace) {
if (namespace === '/') {
return;
}
console.log('Tearing down ' + namespace + ' because of flush');
delete io.nsps[namespace];
delete roomNamespaces[namespace];
});
});
server.get('/rooms', function (req, res, next) {
console.log('Retrieving Room List');
redisClient
.getAsync('rooms')
.then(JSON.parse)
.then(function (rooms) {
console.log('Got Room List');
res.send(rooms || []);
})
.catch(next);
});
server.post('/rooms/:room', function (req, res, next) {
let roomName = req.params.room;
console.log('Creating Room ' + roomName);
redisClient
.getAsync('rooms')
.then(JSON.parse)
.then(function (rooms) {
rooms = rooms || [];
if (rooms.indexOf(roomName) >= 0) {
console.log('Room ' + roomName + ' already exists');
res.status(419);
return res.send({
name: 'RoomAlreadyExists',
message: 'The room ' + roomName + ' already exists',
statusCode: 419
});
}
rooms.push(roomName);
console.log('Storing newly created room');
return Promise.all([
redisClient.setAsync('rooms:' + roomName, JSON.stringify(defaultInstruments)),
redisClient.setAsync('rooms', JSON.stringify(rooms))
]).then(function () {
console.log('Room stored, setting it up');
setupRoom(roomName);
res.status(204);
res.end();
});
})
.catch(next);
});
server.del('/rooms/:room', function (req, res) {
let roomName = req.params.room;
console.log('Removing Room ' + roomName);
redisClient
.getAsync('rooms')
.then(JSON.parse)
.then(function (rooms) {
rooms = rooms || [];
let roomIndex = rooms.indexOf(roomName);
if (roomIndex >= 0) {
console.log('Room ' + roomName + ' exists, removing it');
rooms.splice(roomIndex, 1);
return Promise.all([
redisClient.delAsync('rooms:' + roomName),
redisClient.setAsync('rooms', JSON.stringify(rooms))
]).then(function () {
teardownRoom(roomName);
console.log('Deleted room ' + roomName);
res.status(204);
res.end();
});
}
teardownRoom(roomName);
res.status(404);
res.send({
name: 'RoomNotFound',
message: 'The room ' + roomName + ' was not found',
statusCode: 404
});
});
});
server.get('/rooms/:room/instruments', function (req, res) {
let roomName = req.params.room;
console.log('Retrieving instruments for ' + roomName);
redisClient
.getAsync('rooms:' + roomName)
.then(JSON.parse)
.then(function (room) {
if (!room) {
console.log('Room ' + roomName + 'doesn\'t exist');
res.status(404);
return res.send({
name: 'RoomNotFound',
message: 'The room ' + roomName + ' was not found',
statusCode: 404
});
}
console.log('Sending back instruments for room ' + roomName);
res.send(room);
});
});
server.post('/rooms/:room/instruments/:instrument', function (req, res) {
let roomName = req.params.room;
var instrumentName = req.params.instrument;
console.log('Reserving ' + instrumentName + ' in ' + roomName);
redisClient
.getAsync('rooms:' + roomName)
.then(JSON.parse)
.then(function (room) {
if (!room) {
console.log('Can\'t reserve ' + instrumentName + ' in ' + roomName + ' room doesn\'t exist');
res.status(404);
return res.send({
name: 'RoomNotFound',
message: 'The room ' + roomName + ' was not found',
statusCode: 404
});
}
if (!(instrumentName in room)) {
console.log('Can\'t reserve ' + instrumentName + ' in ' + roomName + ' instrument doesn\'t exist');
res.status(404);
return res.send({
name: 'InstrumentNotFound',
message: 'The instrument ' + instrumentName + ' was not found',
statusCode: 404
});
} else if (room[instrumentName]) {
console.log('Can\'t reserve ' + instrumentName + ' in ' + roomName + ' instrument not available');
res.status(412);
return res.send({
name: 'InstrumentNotAvailable',
message: 'The instrument ' + instrumentName + ' has already been reserved',
statusCode: 412
});
}
room[instrumentName] = true;
return redisClient
.setAsync('rooms:' + roomName, JSON.stringify(room))
.then(function () {
console.log('Instrument ' + instrumentName + ' in ' + roomName + ' reserved');
io.of('/' + roomName).emit('instrument reserved', instrumentName);
io.of('/' + roomName).emit('instruments changed', room);
res.status(204);
res.end();
});
});
});
server.del('/rooms/:room/instruments/:instrument', function (req, res) {
let roomName = req.params.room;
var instrumentName = req.params.instrument;
console.log('Releasing ' + instrumentName + ' in ' + roomName);
releaseInstrument(roomName, instrumentName)
.then(function () {
console.log('Released ' + instrumentName + ' in ' + roomName + ' reserved');
res.status(204);
res.end();
})
.catch(function (reason) {
console.log('Failed to release ' + instrumentName + ' in ' + roomName + ' because ' + reason);
console.error(reason);
res.status(reason.statusCode);
return res.send(reason);
});
});
server.listen(port, function () {
console.log('socket.io server listening at %s', server.url);
});
function releaseInstrument(roomName, instrumentName) {
console.log('Releasing Instrument ' + instrumentName + ' in ' + roomName);
return redisClient
.getAsync('rooms:' + roomName)
.then(JSON.parse)
.then(function (room) {
if (!room) {
console.log('Failed Release ' + instrumentName + ' in ' + roomName + ' because room is not found');
return Promise.reject({
name: 'RoomNotFound',
message: 'The room ' + roomName + ' was not found',
statusCode: 404
});
}
if (!(instrumentName in room)) {
console.log('Failed Release ' + instrumentName + ' in ' + roomName + ' because instrument is not found');
return Promise.reject({
name: 'InstrumentNotFound',
message: 'The instrument ' + instrumentName + ' was not found',
statusCode: 404
});
} else if (!room[instrumentName]) {
console.log('Failed Release ' + instrumentName + ' in ' + roomName + ' because instrument is not reserved');
return Promise.reject({
name: 'InstrumentNotReserved',
message: 'The instrument ' + instrumentName + ' has not yet been reserved',
statusCode: 412
});
}
room[instrumentName] = false;
return redisClient
.setAsync('rooms:' + roomName, JSON.stringify(room))
.then(function () {
io.of('/' + roomName).emit('instrument released', instrumentName);
io.of('/' + roomName).emit('instruments changed', room);
})
.return(room);
});
}
function setupRoom(roomName) {
console.log('Setting up room ' + roomName);
let socketInstruments = {};
let nsp = io.of('/' + roomName);
nsp.on('connection', function (socket) {
console.log('Socket ' + socket.id + ' connected');
socket.on('reserved instrument', function (instrument) {
console.log('Instrument ' + instrument + ' has been confirmed reserved in ' + roomName);
socketInstruments[socket.id] = instrument;
});
socket.on('disconnect', function () {
console.log('Socket ' + socket.id + ' disconnected');
// If the socket that disconnected has an instrument, release it
if (socketInstruments[socket.id]) {
console.log('Releasing ' + socketInstruments[socket.id] + ' due to disconnect in ' + roomName);
releaseInstrument(roomName, socketInstruments[socket.id]);
delete socketInstruments[socket.id];
}
});
Object.keys(defaultInstruments).forEach(function (instrument) {
console.log('Setting up ' + instrument + ' for ' + roomName);
socket.on('play ' + instrument, function (sound) {
console.log('Playing ' + sound + ' for ' + instrument + ' in ' + roomName);
io.of('/' + roomName).emit(instrument + ' played', sound);
});
socket.on('stop ' + instrument, function (sound) {
console.log('Stopping ' + sound + ' for ' + instrument + ' in ' + roomName);
io.of('/' + roomName).emit(instrument + ' stopped', sound);
});
});
});
}
function teardownRoom(room) {
console.log('Tearing down room ' + room);
delete io.nsps['/' + room];
delete roomNamespaces[room];
}
module.exports = {
server: server,
port: process.env.PORT || port,
redisClient: redisClient
};
}());