-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
176 lines (137 loc) · 4.06 KB
/
app.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
175
176
/**
* Module dependencies.
*/
var express = require('express');
var app = module.exports = express.createServer(),
io = require('socket.io').listen(app),
util = {
url: require('url'),
crypto: require('crypto')
}
decks = {};
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
io.configure('development', function() {
io.set('log level', 4);
})
io.configure('production', function(){
io.enable('browser client minification'); // send minified client
io.enable('browser client etag'); // apply etag caching logic based on version number
io.set('log level', 1); // reduce logging
});
io.sockets.on('connection', function(client){
client.on('join', function(data){
var id = getIdFromUrl(data.url),
deck = decks[id];
if (!deck) {
decks[id] = deck = defaultDeckState(id);
}
deck.timestamp = Date.now();
if (data.is_master) {
setupMaster(client, deck);
}
else {
setupViewer(client, deck);
}
});
});
// Routes
app.get('/', function(req, res){
res.render('index');
});
app.get('/nodejs', function(req, res) {
res.render('cronopio/nodejs/index', { layout:'cronopio/nodejs/layout' });
});
app.get('/web-dev', function(req, res) {
res.sendfile(__dirname + '/views/arpunk/index.html');
});
app.get('/artedigital', function(req, res) {
res.render('lili/index', { layout: 'lili/layout' });
});
app.get('/express-framework', function(req, res) {
res.render('flav/express/index', { layout: 'flav/express/layout' });
});
app.get('/gpg', function (req, res) {
res.render('cronopio/gpg/index', { layout: 'cronopio/gpg/layout' });
});
app.get('/git', function (req, res) {
res.render('cronopio/git/index', { layout: 'cronopio/git/layout' });
});
app.get('/seguridad-con-firefox', function(req, res) {
res.render('jaruiz33/index', { layout: 'jaruiz33/layout' });
});
app.get('/deb-rpm', function (req, res) {
res.render('flav/deb-rpm/index', { layout: 'flav/deb-rpm/layout'});
});
app.listen(process.env.PORT || 3000);
app.on('listening', function () {
console.log("Presentacion corriendo en el puerto %d", app.address().port);
});
/**
* Funciones para ayudar al socket.io
*/
function setupMaster(client, deck) {
client.on('master', function(data){
if (verifyKey(data.key, data.input)) {
client.emit('master', true);
deck.has_master = true;
//io.sockets.in(deck.id).emit('notify', { master: true });
client.broadcast.emit('notify', { master: true });
client.on('change', function(data){
deck.current = data.current;
deck.timestamp = Date.now();
//io.sockets.in(deck.id).emit('slide', deck.current);
client.broadcast.emit('slide', deck.current);
});
client.on('disconnect', function(){
deck.has_master = false;
//io.sockets.in(deck.id).emit('notify', { master: false });
client.broadcast.emit('notify', { master: false });
});
}
else {
client.emit('master', false);
}
});
}
function setupViewer(client, deck) {
if (deck.has_master) {
client.emit('notify', {master: true, current: deck.current});
}
client.join(deck.id);
}
function getIdFromUrl(url) {
var parsed = util.url.parse(url);
return parsed.hostname + parsed.pathname;
}
function defaultDeckState(id) {
return {
id: id,
current: 0,
has_master: false
};
}
function verifyKey(key, input) {
return (key == util.crypto.createHash('md5').update(input).digest('hex'));
}
function clearInactiveSessions() {
var now = Date.now();
for (var key in decks) {
if (now - decks[key].timestamp > max_inactive_time) {
delete decks[key];
}
}
}