TR
apps/winzik / src / Cast / TvPage.php · commit 6576d6f
<?phpnamespace App\Cast;/** * The WinZik TV screen (assets/cast/tv.html): the song, its cover, the lyrics line by line with the syllable sung * lit (and the translation under a line when there is one), the songs coming next; it plays the song itself * (<audio>) and follows WinZik, the phone / PC being the remote; the TV's own remote works too (OK, ◀ ▶, ▲ ▼). * * One page, two ways to talk to WinZik: * - 'http': opened in a TV / console / tablet browser from WinZik's LAN server (/tv/<code>): it asks the state *   (/c/<token>/s, with its own position) 3 times a second and sends the remote's keys (/c/<token>/k); * - 'cast': WinZik's Google Cast receiver (an App ID registered by the user, the page on HTTPS: *   site/cast/receiver.html, made by receiverFile()): the same messages over the Cast connection, namespace *   urn:x-cast:net.jumphp.winzik (STATE from WinZik, REPORT and COMMAND from the page). */final class TvPage{    public static function template(): string    {        return (string)file_get_contents(__DIR__ . '/../../assets/cast/tv.html');    }    public static function html(string $transport): string    {        $html = self::template();        if ($transport === 'cast') {            $sdk = '<script src="https://www.gstatic.com/cast/sdk/libs/caf_receiver/v3/cast_receiver_framework.js"></script>';            $js = <<<'JS'// Google Cast: WinZik's messages over the Cast connection (this page is WinZik's receiver application)const NS = 'urn:x-cast:net.jumphp.winzik';const ctx = cast.framework.CastReceiverContext.getInstance();const transport = {  command(c) { try { ctx.sendCustomMessage(NS, undefined, Object.assign({ type: 'COMMAND' }, c)); } catch (e) {} },};ctx.addCustomMessageListener(NS, (ev) => { const d = ev.data || {}; if (d.type === 'STATE') { apply(d.state); } });setInterval(() => { try { ctx.sendCustomMessage(NS, undefined, Object.assign({ type: 'REPORT' }, report())); } catch (e) {} }, 500);const options = new cast.framework.CastReceiverOptions();options.disableIdleTimeout = true;options.skipPlayersLoad = true;options.customNamespaces = { [NS]: cast.framework.system.MessageType.JSON };ctx.start(options);JS;        } else {            $sdk = '';            $js = <<<'JS'// a browser of the home network: WinZik's LAN server, asked 3 times a secondconst BASE = '{{BASE}}';let failures = 0;const transport = {  busy: false,  command(c) { fetch(BASE + '/k?' + new URLSearchParams(c), { cache: 'no-store' }).catch(() => {}); },  tick() {    if (this.busy) { return; }    this.busy = true;    fetch(BASE + '/s?' + new URLSearchParams(report()), { cache: 'no-store' }).then((r) => r.json())      .then((s) => { failures = 0; apply(s); })      .catch(() => { if (++failures > 6) { notice('WinZik ne répond plus (le PC ou le téléphone est-il allumé ?)'); } })      .finally(() => { transport.busy = false; });  },};setInterval(() => transport.tick(), 300);JS;        }        return str_replace(['<!--RECEIVER_SDK-->', '/*TRANSPORT*/'], [$sdk, $js], $html);    }    /** site/cast/receiver.html: the page to host on HTTPS for a Cast App ID (AGENTS.md "Cast"). */    public static function receiverFile(): string    {        return __DIR__ . '/../../site/cast/receiver.html';    }}