Files
simonandClaude Opus 5 ad999e1c3e Add party player: YouTube queue with crossfading playback
Two frontends sharing one WebSocket-backed state on the server:

- Player: fullscreen poster background with a small frame (30% of the
  screen) that drifts diagonally and reflects off the edges. Two stacked
  YouTube players hand over with a 5s equal-power crossfade so there is
  never silence between tracks.
- Queue: add by URL, drag to reorder, delete, click to jump. Shows title
  and length per item, highlights what is playing, and tracks remaining
  vs. total playtime.

No API key is required — titles come from YouTube's oEmbed endpoint and
durations from a hidden muted probe player that cues each new video just
to read its length.

The fade is driven by both requestAnimationFrame and the interval tick:
a hidden or throttled tab stops serving frames entirely, which would
otherwise strand a handover half-faded with two tracks audible.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Tg15tLAuT6NrCE1ZxKVgDB
2026-08-09 21:22:13 +02:00

60 lines
1.5 KiB
JavaScript

// Reconnecting WebSocket shared by both frontends. A party laptop that sleeps,
// or a server restart, must not require reloading the tabs.
export function connect({ onMessage, onStatus } = {}) {
let ws = null;
let retry = 500;
const outbox = [];
function open() {
const proto = location.protocol === 'https:' ? 'wss:' : 'ws:';
ws = new WebSocket(`${proto}//${location.host}`);
ws.addEventListener('open', () => {
retry = 500;
onStatus?.('online');
while (outbox.length) ws.send(outbox.shift());
});
ws.addEventListener('message', (event) => {
let msg;
try {
msg = JSON.parse(event.data);
} catch {
return;
}
onMessage?.(msg);
});
ws.addEventListener('close', () => {
onStatus?.('offline');
setTimeout(open, retry);
retry = Math.min(retry * 2, 5000);
});
ws.addEventListener('error', () => ws.close());
}
open();
return {
send(message) {
const payload = JSON.stringify(message);
if (ws && ws.readyState === WebSocket.OPEN) ws.send(payload);
else outbox.push(payload);
},
};
}
/** Seconds → H:MM:SS (or M:SS below an hour). */
export function formatDuration(seconds) {
if (!Number.isFinite(seconds) || seconds < 0) return '?';
const total = Math.round(seconds);
const h = Math.floor(total / 3600);
const m = Math.floor((total % 3600) / 60);
const s = total % 60;
return h > 0
? `${h}:${String(m).padStart(2, '0')}:${String(s).padStart(2, '0')}`
: `${m}:${String(s).padStart(2, '0')}`;
}