-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
111 lines (94 loc) · 2.27 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
var streamer = require('./lib/streamer')
, express = require('express')
, app = express.createServer()
, http = require('http')
, url = require('url')
, fs = require('fs')
, windows = {}
, io = require('socket.io').listen(app);
io.set('log level', 1);
function broadcast(m) {
if (m.window_id && windows[m.window_id]) {
_(windows[m.window_id]).each(function (s) {
if (s) {
console.log("Sent!");
s.emit("message", {msg: m.msg});
}
});
}
}
/**
* Express server
*/
app.use(express.favicon())
.use(express.bodyParser())
.use(express.staticCache())
.use(express['static'](__dirname + '/assets'))
.use(app.router)
.use(express.errorHandler({dumpExceptions: true}))
;
app.set('view options', {layout: false});
app.register('.html', {compile: function (str, _) {
return function (_) {
return str;
};
}});
app.listen(3000);
// le home
app.get('/', function (req, res) {
res.render('home.html');
});
// le webcam
app.get('/record', function (req, res) {
res.render('record.html');
});
// le random gif
app.get('/random', function (req, res) {
var windows = require('./lib/windows')
, keys = Object.keys(windows.storage)
, random_key = _.random(0, keys.length);
if (keys.length) {
res.redirect('/v/' + keys[random_key]);
} else {
// TODO: better
res.writeHead(404);
res.end('There are no windows open at this moment!');
}
});
// le window capture
app.post('/window/:id/capture', function (req, res) {
streamer.capture(req, res);
});
// le view gif
app.get('/v/:id', function (req, res) {
res.render('view.html');
});
// le streamed gif
app.get('/:id.gif', function (req, res) {
streamer.stream(req.param('id'), broadcast, req, res);
});
/**
* General handlers
*/
process.on('uncaughtException', function (err) {
console.log('Caught exception: ' + err);
});
/**
* Socket.io part
*/
io.sockets.on('connection', function (socket) {
var window_id;
socket.on("join", function (id) {
console.log("Joining window", id);
window_id = id;
windows[id] = windows[id] || [];
windows[id].push(socket);
});
socket.on("message", function (m) {
console.log("Emitting", m);
broadcast(m);
});
socket.on("disconnect", function () {
delete(windows[window_id]);
});
});