LearnSpace: full-stack educational whiteboard platform
Node.js/Express backend + vanilla JS frontend. Features: real-time collaborative whiteboard (SSE), multi-page support, LaTeX formulas, shapes/connectors, coordinate systems, number lines, compass, zoom/pan, Catmull-Rom pencil smoothing, ruler/protractor with rotation & resize controls, minimap navigation overlay, auto-measurements, multi-page thumbnails sidebar, PNG export, page templates. Student/teacher workflows: classes, assignments, library, dashboard. Mobile responsive. SQLite (better-sqlite3). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
/* ── SSE registry — shared between controllers ─────────────────────────── */
|
||||
const clients = new Map(); // userId -> Set<res>
|
||||
const db = require('./db/db');
|
||||
|
||||
function addClient(userId, res) {
|
||||
if (!clients.has(userId)) clients.set(userId, new Set());
|
||||
clients.get(userId).add(res);
|
||||
}
|
||||
|
||||
function removeClient(userId, res) {
|
||||
const set = clients.get(userId);
|
||||
if (!set) return;
|
||||
set.delete(res);
|
||||
if (set.size === 0) clients.delete(userId);
|
||||
}
|
||||
|
||||
function emit(userId, data) {
|
||||
const conns = clients.get(userId);
|
||||
if (!conns?.size) return;
|
||||
const payload = `data: ${JSON.stringify(data)}\n\n`;
|
||||
for (const res of conns) {
|
||||
try { res.write(payload); } catch {}
|
||||
}
|
||||
}
|
||||
|
||||
// Heartbeat: detect and remove dead connections every 30s
|
||||
setInterval(() => {
|
||||
for (const [userId, conns] of clients) {
|
||||
for (const res of conns) {
|
||||
try {
|
||||
if (res.writableEnded || res.destroyed) { conns.delete(res); continue; }
|
||||
res.write(': heartbeat\n\n');
|
||||
} catch { conns.delete(res); }
|
||||
}
|
||||
if (conns.size === 0) clients.delete(userId);
|
||||
}
|
||||
}, 30_000).unref();
|
||||
|
||||
/* Broadcast to all members of a class */
|
||||
function emitToClass(classId, data) {
|
||||
const members = db.prepare('SELECT user_id FROM class_members WHERE class_id=?').all(classId);
|
||||
for (const { user_id } of members) emit(user_id, data);
|
||||
}
|
||||
|
||||
/* Returns array of user IDs currently connected via SSE */
|
||||
function getOnlineUserIds() {
|
||||
return [...clients.keys()];
|
||||
}
|
||||
|
||||
module.exports = { addClient, removeClient, emit, emitToClass, getOnlineUserIds };
|
||||
Reference in New Issue
Block a user