Keep the queue across restarts; add shuffle, loop, renaming and live events
- Save the queue to storage/runtime/queue.json and restore it at startup, the interrupted track first; playback resumes once someone is in the voice channel. Shutdown keeps the queued files instead of deleting them. - Shuffle the upcoming tracks; loop mode sends finished tracks back to the end of the queue, except removed or failed ones. - Rename library tracks (queued copies follow), enqueue the whole library, optionally shuffled. - Stream queue and library changes to the web page with Server-Sent Events (GET /api/events). - Sanitise the requester name sent when queueing a library track. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
39d2b6acfb
commit
9fb54798dd
12 changed files with 390 additions and 18 deletions
40
src/web/routes/events.js
Normal file
40
src/web/routes/events.js
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
import { Router } from "express";
|
||||
|
||||
const HEARTBEAT_INTERVAL = 25_000;
|
||||
|
||||
// Flux d'événements (Server-Sent Events) : « queue » envoie l'état de la file à chaque
|
||||
// changement, « library » signale que la bibliothèque a changé. La page s'en sert à la place
|
||||
// d'un rafraîchissement périodique.
|
||||
function eventsRouter({ queue }) {
|
||||
const router = Router();
|
||||
|
||||
router.get("/", (req, res) => {
|
||||
res.writeHead(200, {
|
||||
"Content-Type": "text/event-stream; charset=utf-8",
|
||||
"Cache-Control": "no-cache, no-transform",
|
||||
Connection: "keep-alive",
|
||||
"X-Accel-Buffering": "no",
|
||||
});
|
||||
|
||||
const send = (event, data) => {
|
||||
res.write(`event: ${event}\ndata: ${JSON.stringify(data)}\n\n`);
|
||||
};
|
||||
const onQueueUpdate = (snapshot) => send("queue", snapshot);
|
||||
const onLibraryUpdate = () => send("library", {});
|
||||
|
||||
send("queue", queue.snapshot());
|
||||
queue.on("queueUpdate", onQueueUpdate);
|
||||
queue.on("libraryUpdate", onLibraryUpdate);
|
||||
const heartbeat = setInterval(() => res.write(": ping\n\n"), HEARTBEAT_INTERVAL);
|
||||
|
||||
req.on("close", () => {
|
||||
clearInterval(heartbeat);
|
||||
queue.off("queueUpdate", onQueueUpdate);
|
||||
queue.off("libraryUpdate", onLibraryUpdate);
|
||||
});
|
||||
});
|
||||
|
||||
return router;
|
||||
}
|
||||
|
||||
export { eventsRouter };
|
||||
Loading…
Add table
Add a link
Reference in a new issue