-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
68 lines (55 loc) · 2.1 KB
/
index.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
((require('node-env-file'))(__dirname + '/.env'));
var express = require('express'),
socketIo = require('socket.io'),
lodash = require('lodash'),
roomManager = require('./roomManager'),
sessionManager = require('./sessionManager'),
cbHandler = require('./cbHandler'),
metroboomApp = express(),
gSocket = socketIo.listen(metroboomApp.listen(process.env.PORT_NUMBER));
/**
* CORS header
*/
metroboomApp.use(function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods',
'GET, POST, PUT, PATCH, DELETE, OPTIONS');
res.setHeader('Access-Control-Allow-Headers',
'X-Requested-With, content-type, authorization, accept, origin');
res.setHeader('Access-Control-Allow-Credentials', 'true');
if (req.method === "OPTIONS") {
res.send(200);
} else {
next();
}
});
gSocket.on('connection', function (socket) {
socket.on('createRoom', function (callback) {
var roomName = roomManager.createRoom("traplord");
socket.join(roomName);
sessionManager(gSocket, socket, roomName, "traplord");
cbHandler(roomName, callback);
});
socket.on('joinRoom', function (data, callback) {
if (typeof data !== "object" ||
!lodash.has(data, "username") ||
!lodash.has(data, "roomName") ||
typeof data.username !== "string" ||
data.username.length === 0 ||
typeof data.roomName !== "string" ||
data.roomName.length === 0){
cbHandler(new Error("Username of the joiner and " +
"RoomName is required"), callback);
return;
}
try {
roomManager.addRoomMember(data.roomName, data.username);
} catch (e) {
cbHandler(e, callback);
return;
}
socket.join(data.roomName);
sessionManager(gSocket, socket, data.roomName, data.username);
cbHandler("success", callback);
});
});