-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.js
174 lines (146 loc) · 5.12 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
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
var socketio = require('socket.io'),
static = require('node-static'),
http = require('http'),
_ = require('lodash');
var file = new static.Server('./pub'),
server = http.createServer(function(req, res) {
req.addListener('end', function() {
file.serve(req, res);
}).resume();
}).listen(7654);
var io = socketio.listen(server);
/////////////////////////////////////
var socketDataFieldName = '_data';
function sendError(socket, msg) {
socket.emit('server_error', {
msg: msg
});
}
function setData(socket, data) {
if (!socket[socketDataFieldName]) {
socket[socketDataFieldName] = {};
}
for (var key in data) {
socket[socketDataFieldName][key] = data[key];
}
}
function getData(socket, fields) {
var ret = {};
if (typeof socket[socketDataFieldName] === 'undefined') {
return undefined;
}
if (! (fields instanceof Array)) {
fields = [fields];
}
fields.forEach(function(fieldName) {
ret[fieldName] = socket[socketDataFieldName][fieldName];
});
return ret;
}
function isUsernameValid(username) {
return /^[a-z0-9_\.-]+$/i.test(username);
}
function generateRoomName() {
return Math.floor(Math.random() * 9000 + 1000).toString(36);
}
function isAuthorized(socket) {
return socket[socketDataFieldName] ? !!socket[socketDataFieldName].name : false;
}
function roomExists(roomName) {
console.log('Rooms=', io.sockets.manager.rooms);
if (roomName == '') {
return false; // no access to empty room
}
return ('/' + roomName) in io.sockets.manager.rooms;
}
function isVoteValid(vote) {
return [0, 0.5, 1, 1.5, 2, 3, 5, 5, 5, 5, 5, 8, 13].indexOf(vote) != -1;
}
function isUserIn(socket, roomName) {
return io.sockets.clients(roomName).indexOf(socket) != -1;
}
io.sockets.on('connection', function(socket) {
console.log('connected', socket.id);
socket.on('set_name', function(data) {
if (isAuthorized(socket)) {
return sendError(socket, 'Already authorized');
}
if (!data) { return; }
if (!data.name || !isUsernameValid(data.name)) {
return sendError(socket, 'Username is empty or invalid');
}
setData(socket, { name: data.name, id: socket.id });
socket.emit('hello', { id: socket.id });
});
////////////////////////////////////////////////////////////
socket.on('join', function(data) {
console.log('JOIN', data);
if (!isAuthorized(socket)) {
return sendError(socket, 'Not authorized');
}
var roomName;
if ( data && (roomName = data.roomName) ) {
if (!roomExists(roomName)) {
return sendError(socket, 'Room with such name does not exist');
}
} else {
roomName = generateRoomName();
}
setData(socket, { vote: undefined });
// @TODO: check if this user is already inside
socket.join(roomName);
socket.emit('welcome', {
roomName: roomName,
users: _.map(io.sockets.clients(roomName), function(s) {
return getData(s, ['id', 'name', 'vote' ])
})
});
io.sockets.in(roomName).emit('joined', getData(socket, ['id', 'name', 'vote']));
});
////////////////////////////////////////////////////////////
socket.on('vote', function(data) {
if (!isAuthorized(socket)) {
return sendError(socket, 'Not authorized');
}
if (!data) { return; }
var roomName = data.roomName,
vote = data.vote;
if (!isUserIn(socket, roomName)) {
return sendError(socket, 'You are not in this room');
}
if (!isVoteValid(vote)) {
return sendError(socket, 'Your vote is invalid');
}
setData(socket, {vote:vote});
var data2send = getData(socket, ['id']);
data2send.vote = vote;
io.sockets.in(roomName).emit('user_voted', data2send);
});
////////////////////////////////////////////////////////////
socket.on('reset', function(data) {
if (!isAuthorized(socket)) {
return sendError(socket, 'Not authorized');
}
if (!data) { return; }
var roomName = data.roomName;
if (!isUserIn(socket, roomName)) {
return sendError(socket, 'You are not in this room');
}
setData(socket, { vote: undefined });
io.sockets.in(roomName).emit('reset', getData(socket, ['id']));
});
////////////////////////////////////////////////////////////
socket.on('disconnect', function() {
if (!isAuthorized(socket)) {
return; // no reason to send error message here;
}
// find all rooms where client was
_.filter(Object.keys(io.sockets.manager.roomClients[socket.id]), function(val) {
return val != '';
}).forEach(function(roomName) {
// every room begins with /
io.sockets.in(roomName.substr(1)).emit('quit', getData(socket, ['id']));
});
socket.emit('disconnect', getData(socket, 'id'));
});
});