mirror of
https://github.com/Moepchi/webspeak3.git
synced 2026-12-01 00:42:49 +08:00
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 <noreply@anthropic.com>
This commit is contained in:
1 parent
9b83e9d93f
commit
fe1b5ff991
5 files changed
+283
-5
No files matched your search
@@ -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
|
||||
@@ -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 {
|
||||
|
||||
+22
-5
@@ -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<string>(OPTIONS_SECTIONS[0].id);
|
||||
const socketRef = useRef<WebSocket | null>(null);
|
||||
const socketRef = useRef<WebSocket | DemoSocket | null>(null);
|
||||
const connectionsMenuRef = useRef<HTMLDivElement | null>(null);
|
||||
const favoritesMenuRef = useRef<HTMLDivElement | null>(null);
|
||||
const awayMenuRef = useRef<HTMLDivElement | null>(null);
|
||||
@@ -1425,6 +1428,10 @@ function AppInner() {
|
||||
const micCaptureRef = useRef<MicCapture | null>(null);
|
||||
const activeTabRef = useRef<ActiveTab>("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<ClientInfo[] | null>(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 (
|
||||
<div className={`ts-app ts-theme-${theme}`}>
|
||||
{DEMO_MODE && (
|
||||
<div className="ts-demo-banner">
|
||||
Demo mode — simulated data only, no real TeamSpeak server involved.{" "}
|
||||
<a href="https://github.com/Moepchi/webspeak3" target="_blank" rel="noreferrer">
|
||||
Get WebSpeak3
|
||||
</a>
|
||||
</div>
|
||||
)}
|
||||
<div className="ts-menubar">
|
||||
<div className="ts-menubar-dropdown" ref={connectionsMenuRef}>
|
||||
<span
|
||||
|
||||
@@ -0,0 +1,185 @@
|
||||
// Standalone frontend demo: no gateway, no real TeamSpeak server. Everything
|
||||
// below simulates the WebSocket protocol the real gateway speaks (see
|
||||
// gateway/src/index.ts) so App.tsx doesn't need to know it isn't talking to
|
||||
// a real backend. Only enabled in the GitHub Pages build (VITE_DEMO_MODE=true)
|
||||
// so the normal Docker image is completely unaffected.
|
||||
export const DEMO_MODE = import.meta.env.VITE_DEMO_MODE === "true";
|
||||
|
||||
export const DEMO_HOST = "demo.webspeak3.example";
|
||||
|
||||
interface DemoChannel {
|
||||
id: number;
|
||||
parent: number;
|
||||
order: number;
|
||||
name: string;
|
||||
topic: string;
|
||||
codec: string;
|
||||
maxClients: number | null;
|
||||
hasPassword: boolean;
|
||||
}
|
||||
|
||||
interface DemoClient {
|
||||
id: number;
|
||||
channel: number;
|
||||
name: string;
|
||||
inputMuted: boolean;
|
||||
outputMuted: boolean;
|
||||
inputHardwareEnabled: boolean;
|
||||
away: boolean;
|
||||
awayMessage: string;
|
||||
isChannelCommander: boolean;
|
||||
}
|
||||
|
||||
const DEMO_CHANNELS: DemoChannel[] = [
|
||||
{ id: 1, parent: 0, order: 0, name: "Lobby", topic: "Welcome to the WebSpeak3 demo!", codec: "Opus Voice", maxClients: null, hasPassword: false },
|
||||
{ id: 2, parent: 0, order: 1, name: "Gaming", topic: "", codec: "Opus Voice", maxClients: 10, hasPassword: false },
|
||||
{ id: 3, parent: 0, order: 2, name: "AFK", topic: "", codec: "Opus Voice", maxClients: null, hasPassword: false },
|
||||
];
|
||||
|
||||
const DEMO_NPCS: DemoClient[] = [
|
||||
{ id: 102, channel: 1, name: "Alex", inputMuted: false, outputMuted: false, inputHardwareEnabled: true, away: false, awayMessage: "", isChannelCommander: true },
|
||||
{ id: 103, channel: 2, name: "Sam", inputMuted: false, outputMuted: false, inputHardwareEnabled: true, away: false, awayMessage: "", isChannelCommander: false },
|
||||
{ id: 104, channel: 2, name: "Jordan", inputMuted: true, outputMuted: false, inputHardwareEnabled: true, away: false, awayMessage: "", isChannelCommander: false },
|
||||
{ id: 105, channel: 3, name: "Riley", inputMuted: false, outputMuted: false, inputHardwareEnabled: true, away: true, awayMessage: "brb, grabbing coffee", isChannelCommander: false },
|
||||
];
|
||||
|
||||
const SELF_ID = 101;
|
||||
|
||||
/**
|
||||
* Drop-in stand-in for a real WebSocket. Implements the subset of the
|
||||
* WebSocket interface App.tsx actually uses (onopen/onmessage/onerror/
|
||||
* onclose/send/close/readyState), so it can be assigned to the same
|
||||
* socketRef without any changes to the event-handling code.
|
||||
*/
|
||||
export class DemoSocket {
|
||||
onopen: ((ev: Event) => 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] });
|
||||
}
|
||||
}
|
||||
@@ -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 || '/',
|
||||
})
|
||||
Reference in new issue
Block a user