-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
437 lines (354 loc) · 15.2 KB
/
server.js
File metadata and controls
437 lines (354 loc) · 15.2 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
const express = require('express');
const app = express();
const http = require('http').createServer(app);
const io = require('socket.io')(http);
const path = require('path');
// Serve static files
app.use(express.static(path.join(__dirname, 'public')));
// Main routes
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'login.html'));
});
app.get('/student', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'student.html'));
});
app.get('/console', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'teacher.html'));
});
// Store drawing history for each student and track teachers
const rooms = new Map(); // sessionCode -> { students: Map<username, {history, currentStep}>, teacherSocketId: string }
io.on('connection', (socket) => {
console.log('User connected with ID:', socket.id);
// Track user session data
let userRoom = null;
let username = null;
let isTeacher = false;
// Handle teacher joining a session
socket.on('teacherJoin', (sessionCode) => {
console.log('Teacher joined with code:', sessionCode);
userRoom = sessionCode;
isTeacher = true;
// Store in socket data
socket.data.isTeacher = true;
// Join the socket.io room
socket.join(userRoom);
// Initialize or update room data
if (!rooms.has(userRoom)) {
rooms.set(userRoom, {
students: new Map(),
teacherSocketId: socket.id
});
console.log('Created new room:', userRoom);
} else {
// Update teacher socket ID if room already exists
rooms.get(userRoom).teacherSocketId = socket.id;
console.log('Updated teacher socket for room:', userRoom);
}
logRoomsStatus();
});
// Handle student joining a session
socket.on('joinRoom', ({ username: user, sessionCode }) => {
console.log('Student attempting to join:', user, 'Room:', sessionCode);
username = user;
userRoom = sessionCode;
// Store username and role in socket data for easy identification
socket.data.username = username;
socket.data.isTeacher = false;
// Check if room exists (teacher is connected)
if (!rooms.has(userRoom)) {
console.log('Room not found, teacher not connected');
socket.emit('teacherDisconnected');
return;
}
// Join the socket.io room
socket.join(userRoom);
// Initialize student data
const room = rooms.get(userRoom);
if (!room.students.has(username)) {
room.students.set(username, {
history: [],
currentStep: -1
});
console.log('Added new student to room:', username);
}
// Notify teacher of new student
if (room.teacherSocketId) {
io.to(room.teacherSocketId).emit('studentJoined', { username });
console.log('Emitted studentJoined event to teacher for:', username);
}
// Send current drawing state to new connection
const studentData = room.students.get(username);
if (studentData.history.length > 0) {
socket.emit('drawingHistory', { history: studentData.history });
}
logRoomsStatus();
});
// Handle drawing events
socket.on('drawBatch', ({ username, sessionCode, batch, paths }) => {
if (!rooms.has(sessionCode)) {
console.log('Draw attempt for non-existent room:', sessionCode);
return;
}
const room = rooms.get(sessionCode);
// Verify student exists in the room
if (!room.students.has(username)) {
console.log('Draw attempt from unknown student:', username);
return;
}
const studentData = room.students.get(username);
if (batch) {
// Add new drawing data to history (batch format for individual strokes)
batch.forEach(data => {
studentData.currentStep++;
studentData.history = studentData.history.slice(0, studentData.currentStep);
studentData.history.push(data);
});
// Broadcast to the teacher
if (room.teacherSocketId) {
io.to(room.teacherSocketId).emit('drawBatch', { username, batch });
}
} else if (paths) {
// Store full paths array (full state updates)
studentData.canvasState = { paths };
console.log(`Received full paths update from ${username} with ${paths.length} paths`);
// Notify the teacher of updated canvas state
if (room.teacherSocketId) {
io.to(room.teacherSocketId).emit('studentCanvasData', {
username,
canvasState: { paths }
});
}
}
});
// Handle synchronization requests from students
socket.on('requestSync', ({ username, sessionCode }) => {
if (!rooms.has(sessionCode)) return;
const room = rooms.get(sessionCode);
if (!room.students.has(username)) return;
console.log(`Sync requested by ${username} in room ${sessionCode}`);
// We don't want to request canvas data from other students
// as that would cause overwriting their work.
// Instead, just send the student's own data if we have it cached
const studentData = room.students.get(username);
if (studentData.canvasState && studentData.canvasState.paths) {
// Send the student their own cached data
socket.emit('syncResponse', {
canvasState: studentData.canvasState
});
console.log(`Sent cached data to ${username} for self-sync`);
} else {
// If no cached data, send empty canvas
socket.emit('syncResponse', {
canvasState: { paths: [] }
});
console.log(`No cached data for ${username}, sent empty canvas`);
}
});
// Handle request for a specific student's canvas data
socket.on('requestStudentCanvas', ({ sessionCode, studentUsername }) => {
if (!rooms.has(sessionCode) || !isTeacher) return;
console.log(`Teacher requesting canvas data for student: ${studentUsername}`);
const room = rooms.get(sessionCode);
// First check if we have cached data
if (room.students.has(studentUsername) &&
room.students.get(studentUsername).canvasState &&
room.students.get(studentUsername).canvasState.paths) {
// Send cached data to the teacher
const studentData = room.students.get(studentUsername);
io.to(socket.id).emit('studentCanvasData', {
username: studentUsername,
canvasState: studentData.canvasState
});
console.log(`Sent cached canvas data for ${studentUsername}`);
}
// Always request fresh data from the student too (if they're connected)
// Find the student's socket if they're connected
if (io.sockets.adapter.rooms.has(sessionCode)) {
const socketsInRoom = [...io.sockets.adapter.rooms.get(sessionCode)];
for (const id of socketsInRoom) {
const studentSocket = io.sockets.sockets.get(id);
if (studentSocket &&
studentSocket.data.username === studentUsername &&
!studentSocket.data.isTeacher) {
// Request canvas data from this student
studentSocket.emit('requestFullCanvas', { forTeacher: true });
console.log(`Requested fresh canvas data from ${studentUsername}`);
return;
}
}
console.log(`Student socket for ${studentUsername} not found, using cached data only`);
}
});
// Handle receiving full canvas data from student
socket.on('fullCanvasData', ({ username, sessionCode, canvasState, forTeacher }) => {
if (!rooms.has(sessionCode)) return;
const room = rooms.get(sessionCode);
console.log(`Received full canvas data from ${username}`);
// Store this data with the student for future
if (room.students.has(username)) {
const studentData = room.students.get(username);
studentData.canvasState = canvasState;
// If this was requested by the teacher, send it only to the teacher
// and include the username to identify which student's data it is
if (forTeacher && room.teacherSocketId) {
io.to(room.teacherSocketId).emit('studentCanvasData', {
username,
canvasState
});
}
}
});
// Handle canvas data for synchronization
socket.on('canvasData', ({ username, sessionCode, canvasState }) => {
if (!rooms.has(sessionCode)) return;
const room = rooms.get(sessionCode);
console.log(`Received canvas data from ${username} with ${canvasState.paths.length} paths`);
// Store this data with the student for future syncs
if (room.students.has(username)) {
const studentData = room.students.get(username);
studentData.canvasState = canvasState;
}
// Only send to teacher, not to other students
if (room.teacherSocketId) {
io.to(room.teacherSocketId).emit('syncResponse', {
username,
canvasState
});
}
});
// Handle erase events
socket.on('erase', ({ username, sessionCode, canvasState }) => {
if (!rooms.has(sessionCode)) return;
const room = rooms.get(sessionCode);
if (!room.students.has(username)) return;
// Store updated canvas state
const studentData = room.students.get(username);
if (canvasState && canvasState.paths) {
studentData.canvasState = canvasState;
}
// Notify the teacher that an erase happened
if (room.teacherSocketId) {
io.to(room.teacherSocketId).emit('erase', {
username,
canvasState
});
}
});
// Handle undo events
socket.on('undo', ({ username, sessionCode, canvasState }) => {
if (!rooms.has(sessionCode)) return;
const room = rooms.get(sessionCode);
if (!room.students.has(username)) return;
const studentData = room.students.get(username);
// Store updated canvas state after undo
if (canvasState && canvasState.paths) {
studentData.canvasState = canvasState;
}
if (studentData.currentStep > 0) {
studentData.currentStep--;
// Notify the teacher
if (room.teacherSocketId) {
io.to(room.teacherSocketId).emit('undo', {
username,
canvasState
});
}
}
});
// Handle clear events
socket.on('clear', ({ username, sessionCode }) => {
if (!rooms.has(sessionCode)) return;
const room = rooms.get(sessionCode);
if (!room.students.has(username)) return;
const studentData = room.students.get(username);
// Reset student's drawing history
studentData.history = [];
studentData.currentStep = -1;
// Update the canvas state to empty
studentData.canvasState = { paths: [] };
// Notify the teacher
if (room.teacherSocketId) {
io.to(room.teacherSocketId).emit('clear', {
username,
canvasState: { paths: [] }
});
}
});
// Handle redo events
socket.on('redo', ({ username, sessionCode, canvasState }) => {
if (!rooms.has(sessionCode)) return;
const room = rooms.get(sessionCode);
if (!room.students.has(username)) return;
const studentData = room.students.get(username);
// Store updated canvas state after redo
if (canvasState && canvasState.paths) {
studentData.canvasState = canvasState;
// Notify the teacher of updated canvas
if (room.teacherSocketId) {
io.to(room.teacherSocketId).emit('redo', {
username,
canvasState
});
}
}
});
// Handle text submissions from students
socket.on('textSubmit', ({ username, sessionCode, text }) => {
if (!rooms.has(sessionCode)) return;
const room = rooms.get(sessionCode);
if (!room.students.has(username)) return;
// Validate text input
if (typeof text !== 'string') return;
const sanitizedText = text.trim().slice(0, 1000);
if (!sanitizedText) return;
// Forward text to the teacher
if (room.teacherSocketId) {
io.to(room.teacherSocketId).emit('textSubmit', { username, text: sanitizedText });
}
});
// Handle disconnections
socket.on('disconnect', () => {
console.log('User disconnected:', socket.id);
if (!userRoom) return;
if (isTeacher) {
// Teacher disconnected - notify all students and clean up
console.log('Teacher disconnected from room:', userRoom);
io.in(userRoom).emit('teacherDisconnected');
rooms.delete(userRoom);
} else if (username) {
// Student disconnected
const room = rooms.get(userRoom);
if (!room) return;
console.log('Student disconnected:', username);
// Notify teacher
if (room.teacherSocketId) {
io.to(room.teacherSocketId).emit('studentLeft', { username });
}
// Clean up student data
room.students.delete(username);
// Clean up empty rooms without teachers
if (room.students.size === 0 && !room.teacherSocketId) {
rooms.delete(userRoom);
}
}
logRoomsStatus();
});
// Helper function to log room status
function logRoomsStatus() {
console.log('\n--- ROOMS STATUS ---');
console.log(`Total rooms: ${rooms.size}`);
rooms.forEach((room, code) => {
console.log(`Room: ${code}, Teacher: ${room.teacherSocketId}, Students: ${room.students.size}`);
room.students.forEach((data, studentName) => {
console.log(` - Student: ${studentName}`);
});
});
console.log('-------------------\n');
}
});
// Start the server
const PORT = process.env.PORT || 3000;
http.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
console.log(`Open http://localhost:${PORT} in your browser`);
});