-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
90 lines (63 loc) · 1.7 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
var io = require('socket.io')
, express = require('express')
, fs = require('fs');
var port = process.env.PORT || 9191;
var app = express.createServer();
outfile = function(path, contenttype, res) {
console.log('outing: ' + path);
fs.readFile(path, function (err, data) {
if( err ) throw err;
else {
res.contentType(contenttype);
res.end( data );
}
});
};
app.get('/', function(req,res) {
outfile('index.htm', 'text/html', res);
});
app.get('/:type/:script', function(req,res) {
var extensions = { scripts: '.js',
styles: '.css' };
var contenttypes = { scripts: 'text/javascript',
styles: 'text/stylesheet' };
var type = req.params.type;
var scriptname = req.params.script;
var extension = extensions[type];
var contenttype = contenttypes[type];
var path = type + "/" + scriptname + extension;
outfile(path, contenttype, res);
});
app.listen(port);
var sio = io.listen(app);
var pomodoro = function() {
var started = false
, done = null,
endPomodoro = function() {
done();
started = false;
},
start = function(duration, ondone, onstarted) {
if(started) return;
done = ondone;
started = true;
setTimeout( endPomodoro, duration * 60 * 1000);
onstarted();
}
return { start: start };
}
var currentpom = new pomodoro();
sio.sockets.on('connection', function (socket) {
var pomodoroduration = 25
, group = "pomradery";
socket.join(group);
var expired = function() {
sio.sockets.in(group).emit("done");
};
socket.on('start', function (data) {
currentpom.start(pomodoroduration, expired, function() {
sio.sockets.in(group).emit("started", { duration: pomodoroduration });
});
});
socket.emit('remaining', { duration: pomodoroduration });
});