-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
176 lines (148 loc) · 5.15 KB
/
server.js
File metadata and controls
176 lines (148 loc) · 5.15 KB
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
const express = require('express');
const http = require('http');
const socketio = require('socket.io');
const short = require('short-uuid');
const app = express();
const server = http.createServer(app);
const io = socketio(server);
app.use(express.static('public'));
// Student page => "/"
app.get('/', (req, res) => {
res.sendFile(__dirname + '/public/index.html');
});
// Teacher console => "/console"
app.get('/console', (req, res) => {
res.sendFile(__dirname + '/public/console.html');
});
// We store exactly one quiz in memory for simplicity
let quiz = {
code: null,
teacherSocket: null,
active: false,
currentQuestion: 0, // 0 means "quiz not started yet"
students: {}
// students[socketId] = { name: "...", typedText: "...(for current question)..." }
};
function genCode() {
return short.generate().substring(0,6).toUpperCase();
}
io.on('connection', (socket) => {
console.log('Connected:', socket.id);
// ---------- TEACHER EVENTS ----------
// 1) Teacher creates brand new quiz
socket.on('teacher-join', () => {
quiz.code = genCode();
quiz.teacherSocket = socket.id;
quiz.active = true;
quiz.currentQuestion = 0;
quiz.students = {};
socket.join(quiz.code);
// Return quiz code to teacher
socket.emit('quiz-code', quiz.code);
console.log(`Teacher joined => code=${quiz.code}`);
});
// 2) Teacher tries to rejoin an existing quiz
socket.on('teacher-rejoin', (rejoinCode) => {
// If we have an active quiz with that code, re-bind
if (quiz.active && quiz.code === rejoinCode) {
quiz.teacherSocket = socket.id; // re-bind teacher
socket.join(quiz.code);
// Send code & current question
socket.emit('quiz-code', quiz.code);
socket.emit('teacher-rejoined', { currentQuestion: quiz.currentQuestion });
// Also send the current student list (names)
const names = Object.values(quiz.students).map(st => st.name);
socket.emit('student-list', names);
console.log(`Teacher rejoined => code=${quiz.code}`);
} else {
// Otherwise, no active quiz => teacher must create a new one
socket.emit('no-active-quiz');
}
});
// 3) Teacher sets new question
socket.on('new-question', (qNum) => {
quiz.currentQuestion = qNum;
// Broadcast to all => "question"
io.to(quiz.code).emit('question', qNum);
console.log(`Teacher => question #${qNum}`);
});
// 4) Teacher ends quiz
socket.on('end-quiz', () => {
if (socket.id === quiz.teacherSocket && quiz.active) {
quiz.active = false;
io.to(quiz.code).emit('quiz-ended');
console.log('Quiz ended by teacher');
}
});
// ---------- STUDENT EVENTS ----------
// a) Student join from scratch
socket.on('student-join', ({ quizCode, studentName }) => {
if (!quiz.active || quizCode !== quiz.code) {
socket.emit('join-failed', 'Invalid code or quiz not active.');
return;
}
quiz.students[socket.id] = { name: studentName, typedText: '' };
socket.join(quizCode);
// Let them know they joined
socket.emit('joined-quiz');
// Immediately tell them the current question
socket.emit('question', quiz.currentQuestion);
// Update teacher’s student list
updateTeacherStudentList();
console.log(`Student "${studentName}" joined => code=${quizCode}`);
});
// b) Student rejoin (page refresh)
socket.on('student-rejoin', ({ quizCode, studentName }) => {
// If quiz is active & code matches => re-add them
if (quiz.active && quiz.code === quizCode) {
quiz.students[socket.id] = { name: studentName, typedText: '' };
socket.join(quizCode);
// joined
socket.emit('joined-quiz');
// also send them the current question
socket.emit('question', quiz.currentQuestion);
updateTeacherStudentList();
console.log(`Student rejoined => code=${quizCode}, name=${studentName}`);
} else {
socket.emit('join-failed', 'Invalid code or quiz not active.');
}
});
// c) Student typing partial text
socket.on('student-typing', (partialText) => {
if (!quiz.active) return;
const st = quiz.students[socket.id];
if (!st) return;
// store it
st.typedText = partialText;
// Send teacher an update with the student's name
io.to(quiz.teacherSocket).emit('student-typing-update', {
studentName: st.name,
partialText
});
});
// ---------- DISCONNECT ----------
socket.on('disconnect', () => {
console.log('Disconnected:', socket.id);
// If teacher => end quiz
if (quiz.teacherSocket === socket.id && quiz.active) {
quiz.active = false;
io.to(quiz.code).emit('quiz-ended');
console.log('Teacher left => quiz ended');
}
// If student => remove
if (quiz.students[socket.id]) {
delete quiz.students[socket.id];
updateTeacherStudentList();
}
});
});
// Update teacher with all student names
function updateTeacherStudentList() {
if (!quiz.teacherSocket) return;
const names = Object.values(quiz.students).map(st => st.name);
io.to(quiz.teacherSocket).emit('student-list', names);
}
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});