heystreamer/src/ws.js
Bashy cdcd3737a7 Wire it up: Twitch events, dashboard, overlay parts, layout editor removed
Twitch / server
- bits via channel.bits.use (cheers + Power-ups, replaces channel.cheer); built-in
  channel point rewards; polls; Hype Train v2; outgoing raids. New scopes:
  channel:read:polls, channel:read:hype_train, channel:manage:raids (log in again).
- Hype Train: alerts on start / each new level / end, plus the progress bar.
- Raid send-off: start/cancel raids from the dashboard (Helix /raids); the card
  stays until channel.raid reports the raid happened.
- StreamElements client started on boot; tip alerts.

Dashboard
- Twitch + StreamElements status, test buttons (poll, Hype Train bar, raid card),
  Raid box, activity Reload + auto-refresh, Redeems in the nav.
- Two-click delete instead of browser confirm() on Media and Actions.

Overlay
- ?parts= splits the overlay into OBS sources: chat, alerts, poll, hypebar, raid,
  or exclusions like ?parts=nochat. Recommended: ?parts=chat (audio Monitor Only,
  chat sounds stay off stream) + ?parts=nochat.
- Now playing switched off (commented, see CLAUDE.md to re-enable).

Removed the layout editor and preview page; chat position/style is baked into
overlay.ejs from the last saved layout (overlay_layout table kept, unused).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 23:18:15 +02:00

46 lines
1.7 KiB
JavaScript

const { WebSocket } = require('ws');
const clients = new Set();
// Overlay -> server messages. Only accepted from connections that present the overlay UUID
// (?overlay=<uuid>, the same secret as the OBS URL), so strangers cannot inject alerts.
function handleIncoming(raw) {
let msg;
try { msg = JSON.parse(raw); } catch { return; }
if (msg?.type === 'chat_notice') {
const alerts = require('./alerts'); // lazy: alerts.js requires this module
alerts.send(alerts.fromOverlayNotice(msg));
}
}
function init(wss) {
wss.on('connection', (ws, req) => {
clients.add(ws);
const db = require('./db');
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}`);
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();
if (np) ws.send(JSON.stringify({ type: 'nowplaying', np }));
const raid = require('./alerts').getPendingRaid(); // overlay reloaded during a raid countdown
if (raid) ws.send(JSON.stringify({ type: 'raid_card', phase: 'start', raid }));
ws.on('close', () => {
clients.delete(ws);
console.log(`[ws] Client disconnected — total: ${clients.size}`);
});
ws.on('error', () => clients.delete(ws));
});
}
function broadcast(data) {
const msg = JSON.stringify(data);
for (const client of clients) {
if (client.readyState === WebSocket.OPEN) {
client.send(msg);
}
}
}
module.exports = { init, broadcast };