-
Notifications
You must be signed in to change notification settings - Fork 3
/
jacss_server.js
128 lines (116 loc) · 4.17 KB
/
jacss_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
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs');
var port = 8088;
app.listen(port);
function handler(req, res) {
//console.log(req);
var file = req.url;
if (file[0] != '/') file = '/' + file;
if (file == '/') {
console.log("root");
res.writeHead(302, {
"Location": "/examples/shared/small.html"
});
res.end("Please go to /examples/shared/small.html");
return;
}
fs.readFile(__dirname + file,
function (err, data) {
if (err) {
if (err.code == 'ENOENT') {
res.writeHead(404);
return res.end('Not found');
}
console.log(err);
res.writeHead(500);
return res.end('Error loading file');
}
res.writeHead(200);
res.end(data);
return true;
});
}
var presentations = {};
var broadcast = function (presentation, event, data) {
presentation.joins.forEach(function (client) {
client.emit(event, data);
});
};
var numberOfKeys = function (obj) {
var r = 0;
for (var x in obj) if (Object.prototype.hasOwnProperty.call(obj, x)) r++;
return r;
};
io.sockets.on('connection', function (socket) {
socket.emit('server.status', {version: '0.0.3', presentations: numberOfKeys(presentations)});
socket.on('register', function (data, fn) {
var name = data.name;
var presentation = presentations[data.name];
socket.set('name', data.name, function () {
fn({presenter: presentation && !!presentation.presenter});
});
});
var ensurePresentation = function (cb) {
socket.get('name', function (err, name) {
if (!err && name) {
var presentation = presentations[name];
if (!presentation) presentation = presentations[name] = { presenter: null, joins: [], stage: -1};
cb(presentation, name);
}
});
};
socket.on('share', function (data, fn) {
ensurePresentation(function (presentation) {
if (presentation.presenter) return fn({error: "There is already presenter"});
presentation.presenter = socket;
presentation.stage = data.stage;
broadcast(presentation, 'presenter', {presenter: true, stage: presentation.stage});
return fn({ok: true});
});
return true;
});
socket.on('stop', function (data, fn) {
ensurePresentation(function (presentation) {
if (presentation.presenter == socket) {
presentation.presenter = null;
broadcast(presentation, 'presenter', {presenter: false});
fn({ok: true});
} else fn({error: "You're not presenter"});
});
});
socket.on('join', function (data, fn) {
ensurePresentation(function (presentation) {
presentation.joins.push(socket);
fn({ok: true, presenter: !!presentation.presenter, stage: presentation.stage});
});
});
socket.on('stage', function (data) {
ensurePresentation(function (presentation) {
presentation.stage = data.stage;
broadcast(presentation, 'stage', {stage: data.stage});
});
});
socket.on('event', function (data) {
ensurePresentation(function (presentation) {
broadcast(presentation, 'event', data);
});
});
socket.on('disconnect', function () {
ensurePresentation(function (presentation, name) {
if (presentation.presenter == socket) {
presentation.presenter = null;
broadcast(presentation, 'presenter', {presenter: false});
}
else {
var i = presentation.joins.indexOf(socket);
if (i >= 0) presentation.joins.splice(i, 1);
}
if (!presentation.presenter && presentation.joins.length == 0) {
delete presentations[name];
console.log("CLEANUP: Removed " + name);
}
})
});
});
console.log("Server running on " + port);