From fe1b5ff99169f779187bebb1287bcb505e9685cf Mon Sep 17 00:00:00 2001 From: Moepchi Date: Sat, 1 Aug 2026 00:19:22 +0200 Subject: [PATCH] Add a static demo mode for a GitHub Pages showcase GitHub Pages only serves static files, but the real app needs a Node gateway and Rust connector to speak the TS3 protocol - neither of which can run there. This adds a fake WebSocket (DemoSocket) that simulates a connection with canned channels/clients entirely client-side, gated behind VITE_DEMO_MODE so the normal Docker build is unaffected, plus a GitHub Actions workflow that builds and publishes it to Pages. Also fixes a real bug surfaced while testing the disconnect flow: a clean disconnect incorrectly showed "Connection closed before the server responded" because the socket's onclose handler couldn't tell a clean disconnect apart from one that never connected. Co-Authored-By: Claude Sonnet 5 --- .github/workflows/deploy-demo.yml | 57 +++++++++ web/src/App.css | 16 +++ web/src/App.tsx | 27 ++++- web/src/demoMode.ts | 185 ++++++++++++++++++++++++++++++ web/vite.config.ts | 3 + 5 files changed, 283 insertions(+), 5 deletions(-) create mode 100644 .github/workflows/deploy-demo.yml create mode 100644 web/src/demoMode.ts diff --git a/.github/workflows/deploy-demo.yml b/.github/workflows/deploy-demo.yml new file mode 100644 index 0000000..dcc235f --- /dev/null +++ b/.github/workflows/deploy-demo.yml @@ -0,0 +1,57 @@ +name: Deploy demo to GitHub Pages + +on: + push: + branches: [main] + paths: + - "web/**" + - ".github/workflows/deploy-demo.yml" + workflow_dispatch: + +permissions: + contents: read + pages: write + id-token: write + +concurrency: + group: pages + cancel-in-progress: true + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-node@v4 + with: + node-version: 20 + cache: npm + cache-dependency-path: web/package-lock.json + + - name: Install dependencies + working-directory: web + run: npm ci + + - name: Build (demo mode) + working-directory: web + env: + VITE_DEMO_MODE: "true" + VITE_BASE_PATH: /webspeak3/ + run: npm run build + + - uses: actions/configure-pages@v5 + + - uses: actions/upload-pages-artifact@v3 + with: + path: web/dist + + deploy: + needs: build + runs-on: ubuntu-latest + environment: + name: github-pages + url: ${{ steps.deployment.outputs.page_url }} + steps: + - id: deployment + uses: actions/deploy-pages@v4 diff --git a/web/src/App.css b/web/src/App.css index 08b6f8e..349bd43 100644 --- a/web/src/App.css +++ b/web/src/App.css @@ -52,6 +52,22 @@ color-scheme: dark; } +/* Demo mode banner */ + +.ts-demo-banner { + flex-shrink: 0; + padding: 0.35rem 0.75rem; + background: #7a4d00; + color: #fff; + font-size: 12px; + text-align: center; +} + +.ts-demo-banner a { + color: #fff; + text-decoration: underline; +} + /* Toolbar */ .ts-menubar { diff --git a/web/src/App.tsx b/web/src/App.tsx index 6d20357..24526d4 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -10,6 +10,7 @@ import { pickAudioOutputDevice, } from "./voice"; import { LanguageProvider, useLanguage, useT, type LangPref } from "./i18n"; +import { DEMO_HOST, DEMO_MODE, DemoSocket } from "./demoMode"; import { SOUND_EVENTS, clearCustomSound, @@ -1351,8 +1352,10 @@ function OptionsDialog({ } function AppInner() { - const [host, setHost] = useState(() => localStorage.getItem(LAST_HOST_KEY) ?? "localhost"); - const [nickname, setNickname] = useState(() => localStorage.getItem(LAST_NICKNAME_KEY) ?? ""); + const [host, setHost] = useState(() => localStorage.getItem(LAST_HOST_KEY) ?? (DEMO_MODE ? DEMO_HOST : "localhost")); + const [nickname, setNickname] = useState( + () => localStorage.getItem(LAST_NICKNAME_KEY) ?? (DEMO_MODE ? "Guest" : "") + ); const [serverPassword, setServerPassword] = useState(""); const [channelPassword, setChannelPassword] = useState(""); const [defaultChannel, setDefaultChannel] = useState(""); @@ -1414,7 +1417,7 @@ function AppInner() { const [extrasMenuOpen, setExtrasMenuOpen] = useState(false); const [optionsDialogOpen, setOptionsDialogOpen] = useState(false); const [optionsSection, setOptionsSection] = useState(OPTIONS_SECTIONS[0].id); - const socketRef = useRef(null); + const socketRef = useRef(null); const connectionsMenuRef = useRef(null); const favoritesMenuRef = useRef(null); const awayMenuRef = useRef(null); @@ -1425,6 +1428,10 @@ function AppInner() { const micCaptureRef = useRef(null); const activeTabRef = useRef("channel"); const hasConnectedRef = useRef(false); + // Set when a "disconnected" event is received, so the socket's onclose + // handler (which fires shortly after, on its own close handshake) can tell + // a clean disconnect apart from the socket dying before ever connecting. + const cleanDisconnectRef = useRef(false); const previousClientsRef = useRef(null); const pokeIdRef = useRef(0); const inputMutedRef = useRef(false); @@ -1609,7 +1616,7 @@ function AppInner() { ensureAudioContext(); - const socket = new WebSocket(GATEWAY_URL); + const socket = DEMO_MODE ? new DemoSocket() : new WebSocket(GATEWAY_URL); socketRef.current = socket; socket.onopen = () => { @@ -1706,6 +1713,7 @@ function AppInner() { case "disconnected": { const wasConnected = hasConnectedRef.current; hasConnectedRef.current = false; + cleanDisconnectRef.current = true; previousClientsRef.current = null; setConnecting(false); setConnected(false); @@ -1743,10 +1751,11 @@ function AppInner() { } }; socket.onclose = () => { - if (!hasConnectedRef.current) { + if (!hasConnectedRef.current && !cleanDisconnectRef.current) { setConnecting(false); setConnectError((prev) => prev ?? "Connection closed before the server responded"); } + cleanDisconnectRef.current = false; setConnected(false); stopMic(); audioPlayerRef.current?.dispose(); @@ -2078,6 +2087,14 @@ function AppInner() { return (
+ {DEMO_MODE && ( +
+ Demo mode — simulated data only, no real TeamSpeak server involved.{" "} + + Get WebSpeak3 + +
+ )}
void) | null = null; + onmessage: ((ev: MessageEvent) => void) | null = null; + onerror: ((ev: Event) => void) | null = null; + onclose: ((ev: CloseEvent) => void) | null = null; + readyState: number = WebSocket.CONNECTING; + + private closed = false; + private timers: number[] = []; + private selfChannel = 1; + + constructor() { + this.after(400, () => { + this.readyState = WebSocket.OPEN; + this.onopen?.(new Event("open")); + }); + } + + send(raw: string) { + if (this.closed) return; + let msg: any; + try { + msg = JSON.parse(raw); + } catch { + return; + } + this.handle(msg); + } + + close() { + if (this.closed) return; + // Mirrors the real gateway: a "disconnected" event arrives before the + // socket itself finishes closing (see gateway's disconnected event vs. + // the client-initiated WebSocket close in handleDisconnect). + this.emit({ type: "disconnected", reason: "client requested" }); + this.closed = true; + this.readyState = WebSocket.CLOSED; + this.timers.forEach((id) => window.clearTimeout(id)); + this.timers = []; + this.onclose?.({} as CloseEvent); + } + + private after(ms: number, fn: () => void) { + const id = window.setTimeout(() => { + if (this.closed) return; + fn(); + }, ms); + this.timers.push(id); + } + + private emit(data: unknown) { + if (this.closed) return; + this.onmessage?.({ data: JSON.stringify(data) } as MessageEvent); + } + + private handle(msg: any) { + switch (msg.type) { + case "connect": { + const nickname = String(msg.nickname || "Guest").trim() || "Guest"; + this.after(900, () => { + this.emit({ + type: "connected", + serverName: "WebSpeak3 Demo Server", + serverMaxClients: 32, + serverVersion: "3.13.7 [Build: demo]", + serverLicense: "Demo License", + serverBannerUrl: null, + welcomeMessage: "This is a simulated demo - no real TeamSpeak server is involved. Connect with any name/address you like.", + }); + this.after(150, () => this.sendChannels(nickname)); + }); + break; + } + case "switchChannel": { + this.selfChannel = msg.channelId; + this.after(120, () => this.sendChannels(this.lastNickname)); + break; + } + case "sendChatMessage": + this.after(80, () => this.emit({ type: "chatMessage", from: this.lastNickname, message: msg.message })); + break; + case "sendServerMessage": + this.after(80, () => this.emit({ type: "serverMessage", from: this.lastNickname, message: msg.message })); + break; + case "sendPrivateMessage": { + const partner = DEMO_NPCS.find((c) => c.id === msg.clientId); + this.after(80, () => + this.emit({ + type: "privateMessage", + partnerId: msg.clientId, + partnerName: partner?.name ?? "Unknown", + message: msg.message, + fromSelf: true, + }) + ); + break; + } + case "sendPoke": { + const partner = DEMO_NPCS.find((c) => c.id === msg.clientId); + this.after(400, () => + this.emit({ type: "poke", from: partner?.name ?? "Someone", message: "(demo poke - no reply expected)" }) + ); + break; + } + // "disconnect" is intentionally unhandled here: handleDisconnect() in + // App.tsx always calls socket.close() right after sending it, and + // close() already emits the "disconnected" event below. + // setAway, setInputMuted, setOutputMuted, sendAudio: no observable effect + // in a simulated single-user session, intentionally no-ops. + default: + break; + } + } + + private lastNickname = "Guest"; + + private sendChannels(nickname: string) { + this.lastNickname = nickname; + const self: DemoClient = { + id: SELF_ID, + channel: this.selfChannel, + name: nickname, + inputMuted: false, + outputMuted: false, + inputHardwareEnabled: true, + away: false, + awayMessage: "", + isChannelCommander: false, + }; + this.emit({ type: "channels", channels: DEMO_CHANNELS, clients: [self, ...DEMO_NPCS] }); + } +} diff --git a/web/vite.config.ts b/web/vite.config.ts index 8b0f57b..70d81ca 100644 --- a/web/vite.config.ts +++ b/web/vite.config.ts @@ -4,4 +4,7 @@ import react from '@vitejs/plugin-react' // https://vite.dev/config/ export default defineConfig({ plugins: [react()], + // Set by the GitHub Pages workflow to the repo subpath (e.g. /webspeak3/); + // the normal Docker build serves from the domain root and leaves this unset. + base: process.env.VITE_BASE_PATH || '/', })