-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·136 lines (115 loc) · 3.55 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
var express = require('express');
var app = express();
var path = require('path');
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.use(express.static(path.join(__dirname, 'build')));
const DEFAULT_BUTTONS = [
"You are going too fast.",
"You are going too slow.",
"The last slide made no sense.",
"Nice hair!"
];
const RATE_LIMIT = 20*1000;
var lectures = {};
var lecturesCreated = 0;
function generateKey() {
lecturesCreated++;
return lecturesCreated*31+5;
}
io.on('connection', function(socket){
console.log('a user connected');
var room;
var lastClick = 0;
socket.on('create', function(data){
if(data.name != null && data.name != ''){ // add && data.geo != null
var key = generateKey();
lectures[key] = {
owner: socket,
//geo: data.geo,
name: data.name,
buttons: DEFAULT_BUTTONS
};
room = key;
socket.join(key);
socket.emit('set-key', {
key: key
});
socket.emit('set-buttons', {buttons: lectures[key].buttons});
}
});
socket.on('join', function(data){
if(data.key != null){
if(lectures[data.key]){
socket.join(data.key);
room = data.key;
socket.emit('set-buttons', {buttons: lectures[data.key].buttons});
socket.emit('set-name', {
name: lectures[data.key].name
});
lectures[room].owner.emit('student', {action: 'add'});
}
else{
socket.emit('collapse');
}
}
});
socket.on('button', function(data){
if(data.name != null && lectures[room] != null){
if(Date.now() - RATE_LIMIT >= lastClick) {
lectures[room].owner.emit('button', {name: data.name});
lastClick = Date.now();
}
}
});
// LATER
socket.on('set-buttons', function(data){
if(data.buttons != null && socket == lectures[room].owner){
lectures[room].buttons = data.buttons;
socket.to(room).emit('set-buttons', {buttons: data.buttons});
}
});
socket.on('question', function(data){
if(data.text != null && lectures[room] != null){
//if(Date.now() - RATE_LIMIT >= lastClick) {
lectures[room].owner.emit('question', {text: data.text});
//lastClick = Date.now();
//}
}
});
socket.on('disconnect', function(){
if(room != null && lectures[room] != null){
if(socket == lectures[room].owner){
socket.to(room).emit('collapse', {});
lectures[room] = undefined;
}
else{
lectures[room].owner.emit('student', {action: 'remove'});
}
}
});
});
/// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
/// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.json(err);
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.json(err);
});
http.listen(process.env.PORT || 3000, function () {
console.log('RUNNING!')
});