-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
177 lines (155 loc) · 4.51 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
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
177
var connect = require('connect')
var serveStatic = require('serve-static');
var bodyParser = require('body-parser')
var fs = require('fs');
var ejs = require('ejs');
var app = connect()
var http_server = require('http').createServer(app)
var io = require('socket.io')(http_server)
app.use(serveStatic('public'))
.use(bodyParser.urlencoded({extended:true}))
.use(route);
http_server.listen(3000);
//MODEL DATA
rooms = []
sockets = []
id_2_room = []
//CONTROLLER FUNCTIONS
function route(req, res, next) {
switch(req.url){
case '/':
landing(req, res, next);
break;
case '/teacher':
teacher_landing(req, res, next);
break;
case '/student':
student_landing(req, res, next);
break;
case '/teacher_room':
teacher_room(req, res, next);
break;
case '/student_room':
student_room(req, res, next);
break;
case '/submit_code':
submit_code(req, res, next);
break;
default:
res.end('Bad request')
}
}
function landing(req, res, next) {
serve_html('./html/landing.html', {}, req, res, next);
}
function teacher_landing(req, res, next) {
serve_html('./html/teacher_landing.html', {message : ''}, req, res, next);
}
function student_landing(req, res, next) {
serve_html('./html/student_landing.html', {message : ''}, req, res, next);
}
function teacher_room(req, res, next) {
if(req.body.password && req.body.room_name) {
passwd = req.body.password;
room_n = req.body.room_name;
if(rooms[room_n]) {
//room already exists
serve_html('./html/teacher_landing.html', {message : 'Room name already exists.'}, req, res, next);
}
else {
//adjust model data
rooms[room_n] = passwd
//serve html
serve_html('./html/teacher_room.html', {room_name : room_n}, req, res, next);
//here client makes connection with io socket (see socket.io listeners)
}
} else {
//Not all fields filled in
serve_html('./html/teacher_landing.html', {message : 'Please fill in all fields.'}, req, res, next);
}
}
function student_room(req, res, next) {
if(req.body.room_name) {
room_n = req.body.room_name;
if(rooms[room_n]) {
//room exists
serve_html('./html/student_room.html', {room_name : room_n, message : ''}, req, res, next);
}
else {
//room does not exist
serve_html('./html/student_landing.html', {message : 'Room does not exist (yet). Please wait or try again.'}, req, res, next);
}
} else {
//Not all fields filled in
serve_html('./html/student_landing.html', {message : 'Please fill in all fields.'}, req, res, next);
}
}
function submit_code(req, res, next) {
if(req.body.password && req.body.code_example) {
room_n = req.body.room_name;
passwd = req.body.password;
code = req.body.code_example;
if(rooms[room_n]) {
//room exists
//check password
if(passwd != rooms[room_n]){
serve_html('./html/student_room.html', {room_name : room_n, message : 'Wrong password.'}, req, res, next);
} else {
if(req.body.to_project){
//emit code project event
console.log('Project code to ' + room_n)
io.in(room_n).emit('project', {
example: code,
room: room_n,
created: Date.now()
});
} else {
//log -> TODO
}
//serve html
serve_html('./html/student_room.html', {room_name : room_n, message: ''}, req, res, next);
}
} else {
//room does not exist
serve_html('./html/student_landing.html', {message : 'Room does not exist (yet). Please wait or try again.'}, req, res, next);
}
} else if(req.body.room_name){
//Not all fields filled in
serve_html('./html/student_room.html', {room_name : room_n, message : 'Please fill in all fields.'}, req, res, next);
} else {
//Bad request
serve_html('./html/student_landing.html', {message : ''}, req, res, next);
}
}
//VIEW FUNCTIONS
function serve_html(file, params, req, res, next) {
fs.readFile(file, {encoding:'utf-8'}, function(error, content) {
if (error) {
res.writeHead(500);
res.end();
}
else {
res.writeHead(200, { 'Content-Type': 'text/html' });
var rendered_html = ejs.render(content, params);
res.end(rendered_html, 'utf-8');
}
});
}
//SOCKET.IO LISTENERS
io.sockets.on('connection', function(socket){
socket.on('set_room', function(data){
room_n = data.room_name;
//register socket and join room
sockets[room_n] = socket;
id_2_room[socket.id] = room_n;
socket.join(room_n);
console.log('Connection to ' + room_n);
});
socket.on('disconnect', function(){
room_n = id_2_room[socket.id]
delete rooms[room_n];
delete sockets[room_n];
delete id_2_room[socket.id]
console.log('Disconnect from ' + room_n)
});
});