diff --git a/CLAUDE.md b/CLAUDE.md index c58fa05..3f936ee 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -135,6 +135,13 @@ Chat (`?parts=chat`) is `public/chat/chat.css` + `public/chat/chat.js` (window.H - Moderation: deleted messages, timeouts, bans and /clear remove the matching slips at once. - Design preview with other styles: `~/Desktop/Stream/chat/index.html` (not in the repo). +## Overlay updates after a deploy + +OBS keeps an overlay page running for hours, so after a deploy it would keep the old alert code. `src/ws.js` has a +`BOOT_ID` (new on every server start); the overlay page is rendered with it, and the server sends +`{type:'hello', boot}` on every WebSocket connect. When the overlay reconnects after a restart and the ID differs, it +reloads itself (1.5 s later). Asset URLs in overlay.ejs carry `?v=` so the reload never uses cached files. + ## TTS voices (/tts) Builder page to make named TTS voices, used by redeem variations (Redeems page: "TTS: " per variation). diff --git a/src/routers/overlay.js b/src/routers/overlay.js index 6c1421f..e9f7579 100644 --- a/src/routers/overlay.js +++ b/src/routers/overlay.js @@ -1,6 +1,7 @@ const express = require('express'); const router = express.Router(); const db = require('../db'); +const ws = require('../ws'); // Parts of the overlay. One OBS browser source can show all of them, or the overlay can be split into // several sources with ?parts=, e.g. chat in its own source (audio "Monitor Only" in OBS, so chat @@ -32,7 +33,7 @@ router.get('/:uuid', (req, res) => { const settings = db.getOverlaySettings(); if (req.params.uuid !== settings.overlay_id) return res.status(404).send('Overlay not found'); res.render('overlay', { layout: false, settings, parts: partsFrom(req.query.parts), - chatBox: chatBoxFrom(req.query.chat), chatFlow: req.query.chatflow === 'up' ? 'up' : 'down' }); + chatBox: chatBoxFrom(req.query.chat), chatFlow: req.query.chatflow === 'up' ? 'up' : 'down', boot: ws.BOOT_ID }); }); module.exports = router; diff --git a/src/ws.js b/src/ws.js index 3e07f52..2e75b99 100644 --- a/src/ws.js +++ b/src/ws.js @@ -1,6 +1,9 @@ const { WebSocket } = require('ws'); const clients = new Set(); +// changes every server start (= every deploy): overlays compare it with the one their page was built with +// and reload themselves when it differs, so OBS never keeps running old alert code after an update +const BOOT_ID = Date.now().toString(36); // Overlay -> server messages. Only accepted from connections that present the overlay UUID // (?overlay=, the same secret as the OBS URL), so strangers cannot inject alerts. @@ -20,6 +23,7 @@ function init(wss) { const overlayId = new URL(req.url, 'http://x').searchParams.get('overlay'); const trusted = !!overlayId && overlayId === db.getSettings().overlay_id; console.log(`[ws] Client connected${trusted ? ' (overlay)' : ''} — total: ${clients.size}`); + ws.send(JSON.stringify({ type: 'hello', boot: BOOT_ID })); if (trusted) ws.on('message', data => handleIncoming(String(data).slice(0, 2000))); // a freshly loaded overlay gets the current song right away const np = require('./nowplaying').get(); @@ -43,4 +47,4 @@ function broadcast(data) { } } -module.exports = { init, broadcast }; +module.exports = { BOOT_ID, init, broadcast }; diff --git a/views/overlay.ejs b/views/overlay.ejs index 694be44..49e2ec9 100644 --- a/views/overlay.ejs +++ b/views/overlay.ejs @@ -6,14 +6,14 @@ <% const has = p => parts.includes(p); %> <% if (has('alerts')) { %> - - - + + + <% } %> - <% if (has('chat')) { %><% } %> - <% if (has('poll')) { %><% } %> - <% if (has('hypebar')) { %><% } %> - <% if (has('raid')) { %><% } %> + <% if (has('chat')) { %><% } %> + <% if (has('poll')) { %><% } %> + <% if (has('hypebar')) { %><% } %> + <% if (has('raid')) { %><% } %> <%# Now playing (Feishin) is switched off for now. To bring it back: add 'nowplaying' to PARTS in src/routers/overlay.js, uncomment the /nowplaying route in src/app.js and restore the NOWPLAYING lines here: (inside an if (has('nowplaying'))) @@ -72,15 +72,15 @@ <% } %> - <% if (has('chat')) { %><% } %> + <% if (has('chat')) { %><% } %> <% if (has('alerts')) { %> - - - + + + <% } %> - <% if (has('poll')) { %><% } %> - <% if (has('hypebar')) { %><% } %> - <% if (has('raid')) { %><% } %> + <% if (has('poll')) { %><% } %> + <% if (has('hypebar')) { %><% } %> + <% if (has('raid')) { %><% } %>