forked from rdepena/planningpoker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
118 lines (103 loc) · 4.03 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
/*jslint indent: 4, maxerr: 50, vars: true, nomen: true*/
/*global */
(function () {
'use strict';
var http = require('http'),
express = require('express'),
path = require('path'),
roomCache = require('./src/roomsCache'),
app = express(),
port = process.env.PORT || 5000;
var clients = {};
//configure the express app
app.configure(function () {
app.set('views', __dirname + '/views');
app.engine('html', require('ejs').renderFile);
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.static(path.join(__dirname, 'public')));
app.use(app.router);
});
app.configure('development', function () {
app.use(express.errorHandler());
});
//Routes:
app.get('/', function (req, res) {
res.render('index.html');
});
app.get('/end', function (req, res) {
res.render('end.html');
});
app.get('*', function (req, res) {
res.render('404.html');
});
//Start the server:
var server = http.createServer(app).listen(port, function () {
console.log("express server listening on port " + port);
});
//Sockets
var io = require('socket.io').listen(server);
io.sockets.on('connection', function (socket) {
clients[socket.id] = socket;
socket.on('broadcast', function (data) {
//get the roomName from the payload.
var roomName = data.room,
user = {
name : data.message.name || null,
vote : data.message.vote || null,
socketId : data.message.socketId || this.id
};
//if a user joins:
if (data.message.eventType === 'join') {
//we add the user to the room:
data.message.socketId = this.id;
roomCache.addUpdateUser(roomName, user);
//respond to the user who joined with the curroomName status.
socket.emit('event', {
message : {
eventType : 'roomStatus',
room : roomCache.getRoomByName(roomName)
}
});
} else if (data.message.eventType === 'vote') {
//if a user votes:
roomCache.addUpdateUser(roomName, user);
} else if (data.message.eventType === 'toggle') {
//if the visibility of the votes has changed.
roomCache.updateVoteVisibility(roomName, data.message.reveal);
} else if (data.message.eventType === 'reset') {
//if the vote reset was effected.
roomCache.resetVotes(roomName);
} else if (data.message.eventType == 'kick') {
roomCache.kick(roomName, user);
clients[user.socketId].emit('event', {
message : {
eventType : 'kicked'
}
});
} else if (data.message.eventType == 'nudge') {
clients[user.socketId].emit('event', {
message : {
eventType : 'nudged'
}
});
} else if (data.message.eventType == 'message') {
clients[user.socketId].emit('event', {
message : {
eventType : 'messaged',
payload : data.message.payload
}
});
}
//we emit the event to the other clients.
io.sockets.in(roomName).emit('event', data);
});
//when user joins the room
socket.on('joinRoom', function (data) {
socket.set('room', data.room, function () {});
socket.join(data.room);
});
});
})();