tag(功能):功能跑通

This commit is contained in:
OrangeRoll
2026-08-07 13:15:26 +08:00
parent 9e0387bf94
commit 3d1c6a6c76
19 changed files with 892 additions and 418 deletions
+183 -166
View File
@@ -1,184 +1,201 @@
.counter {
font-size: 16px;
padding: 5px 10px;
border-radius: 5px;
color: var(--accent);
background: var(--accent-bg);
border: 2px solid transparent;
transition: border-color 0.3s;
margin-bottom: 24px;
&:hover {
border-color: var(--accent-border);
}
&:focus-visible {
outline: 2px solid var(--accent);
outline-offset: 2px;
}
.app-shell {
width: min(1120px, calc(100% - 32px));
margin: 0 auto;
padding: 56px 0;
}
.hero {
position: relative;
.base,
.framework,
.vite {
inset-inline: 0;
margin: 0 auto;
}
.base {
width: 170px;
position: relative;
z-index: 0;
}
.framework,
.vite {
position: absolute;
}
.framework {
z-index: 1;
top: 34px;
height: 28px;
transform: perspective(2000px) rotateZ(300deg) rotateX(44deg) rotateY(39deg)
scale(1.4);
}
.vite {
z-index: 0;
top: 107px;
height: 26px;
width: auto;
transform: perspective(2000px) rotateZ(300deg) rotateX(40deg) rotateY(39deg)
scale(0.8);
}
header {
margin-bottom: 32px;
}
#center {
display: flex;
flex-direction: column;
gap: 25px;
place-content: center;
place-items: center;
flex-grow: 1;
@media (max-width: 1024px) {
padding: 32px 20px 24px;
gap: 18px;
}
.eyebrow {
color: #6d5dfc;
font-size: 0.78rem;
font-weight: 700;
letter-spacing: 0.12em;
}
#next-steps {
display: flex;
border-top: 1px solid var(--border);
h1 {
margin: 8px 0;
color: #202135;
font-size: clamp(2rem, 5vw, 3.5rem);
}
h2 {
color: #292a40;
font-size: 1.1rem;
}
.subtitle, .development-note {
color: #666980;
}
.join-panel {
display: grid;
grid-template-columns: 1.1fr 1fr 1fr auto;
gap: 14px;
align-items: end;
padding: 20px;
border: 1px solid #e0e1ee;
border-radius: 16px;
background: #fff;
box-shadow: 0 8px 30px rgb(30 33 80 / 7%);
}
label {
display: grid;
gap: 7px;
color: #4a4c62;
font-size: 0.85rem;
font-weight: 600;
text-align: left;
& > div {
flex: 1 1 0;
padding: 32px;
@media (max-width: 1024px) {
padding: 24px 20px;
}
}
.icon {
margin-bottom: 16px;
width: 22px;
height: 22px;
}
@media (max-width: 1024px) {
flex-direction: column;
text-align: center;
}
}
#docs {
border-right: 1px solid var(--border);
@media (max-width: 1024px) {
border-right: none;
border-bottom: 1px solid var(--border);
}
input, select, button {
box-sizing: border-box;
min-height: 42px;
border-radius: 9px;
font: inherit;
}
#next-steps ul {
list-style: none;
padding: 0;
display: flex;
gap: 8px;
margin: 32px 0 0;
.logo {
height: 18px;
}
a {
color: var(--text-h);
font-size: 16px;
border-radius: 6px;
background: var(--social-bg);
display: flex;
padding: 6px 12px;
align-items: center;
gap: 8px;
text-decoration: none;
transition: box-shadow 0.3s;
&:hover {
box-shadow: var(--shadow);
}
.button-icon {
height: 18px;
width: 18px;
}
}
@media (max-width: 1024px) {
margin-top: 20px;
flex-wrap: wrap;
justify-content: center;
li {
flex: 1 1 calc(50% - 8px);
}
a {
width: 100%;
justify-content: center;
box-sizing: border-box;
}
}
}
#spacer {
height: 88px;
border-top: 1px solid var(--border);
@media (max-width: 1024px) {
height: 48px;
}
}
.ticks {
position: relative;
input, select {
width: 100%;
padding: 0 12px;
border: 1px solid #cacbdd;
background: #fff;
color: #292a40;
}
&::before,
&::after {
content: '';
position: absolute;
top: -4.5px;
border: 5px solid transparent;
button {
padding: 0 16px;
border: 0;
cursor: pointer;
font-weight: 700;
}
button:disabled {
cursor: not-allowed;
opacity: 0.55;
}
.primary {
background: #6254e8;
color: #fff;
}
.secondary {
border: 1px solid #c9cae0;
background: #fff;
color: #383a52;
}
.error {
margin: 16px 0 0;
padding: 12px 14px;
border-radius: 8px;
background: #fff0f0;
color: #a12828;
text-align: left;
}
.status-bar {
display: flex;
gap: 16px;
align-items: center;
margin: 24px 0;
color: #585b73;
}
.status {
padding: 5px 10px;
border-radius: 999px;
background: #edeef5;
font-size: 0.85rem;
}
.status.online {
background: #def7e9;
color: #167549;
}
.share-button {
margin-left: auto;
background: #202135;
color: #fff;
}
.hint {
color: #8a6a18;
font-size: 0.86rem;
}
.screens {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 20px;
}
.screen-section {
min-width: 0;
text-align: left;
}
.video-stack {
display: grid;
gap: 12px;
min-height: 280px;
padding: 12px;
border: 1px solid #e0e1ee;
border-radius: 14px;
background: #f6f6fb;
}
.empty {
align-self: center;
color: #797c92;
text-align: center;
}
.screen-frame {
margin: 0;
overflow: hidden;
border-radius: 9px;
background: #181a29;
}
.screen-frame figcaption {
padding: 8px 10px;
color: #e8e9f4;
font-size: 0.82rem;
}
.media-slot video {
display: block;
width: 100%;
max-height: 420px;
background: #10111b;
}
.development-note {
margin: 28px auto 0;
max-width: 780px;
font-size: 0.85rem;
}
@media (max-width: 760px) {
.app-shell {
padding: 32px 0;
}
&::before {
left: 0;
border-left-color: var(--border);
.join-panel, .screens {
grid-template-columns: 1fr;
}
&::after {
right: 0;
border-right-color: var(--border);
.status-bar {
flex-wrap: wrap;
}
.share-button {
margin-left: 0;
}
}
+245 -107
View File
@@ -1,121 +1,259 @@
import { useState } from 'react'
import reactLogo from './assets/react.svg'
import viteLogo from './assets/vite.svg'
import heroImg from './assets/hero.png'
import { useEffect, useRef, useState } from 'react'
import { Room, RoomEvent, Track } from 'livekit-client'
import './App.css'
function App() {
const [count, setCount] = useState(0)
type Role = 'host' | 'publisher' | 'viewer'
type TokenResponse = {
roomName: string
participantIdentity: string
participantName: string
livekitUrl: string
token: string
role: Role
}
type ScreenFeed = {
track: Track
label: string
}
function ScreenTrack({ track, label }: ScreenFeed) {
const mediaContainerRef = useRef<HTMLDivElement>(null)
useEffect(() => {
const mediaElement = track.attach()
mediaElement.autoplay = true
if (mediaElement instanceof HTMLVideoElement) {
// Local preview must be muted for browsers to allow autoplay.
mediaElement.muted = true
mediaElement.playsInline = true
void mediaElement.play().catch(() => {
// The user can still start playback manually if their browser blocks it.
})
}
mediaContainerRef.current?.append(mediaElement)
return () => {
mediaElement.remove()
}
}, [track])
return (
<>
<section id="center">
<div className="hero">
<img src={heroImg} className="base" width="170" height="179" alt="" />
<img src={reactLogo} className="framework" alt="React logo" />
<img src={viteLogo} className="vite" alt="Vite logo" />
</div>
<div>
<h1>Get started</h1>
<p>
Edit <code>src/App.tsx</code> and save to test <code>HMR</code>
</p>
</div>
<button
type="button"
className="counter"
onClick={() => setCount((count) => count + 1)}
>
Count is {count}
</button>
<figure className="screen-frame">
<figcaption>{label}</figcaption>
<div className="media-slot" ref={mediaContainerRef} />
</figure>
)
}
function App() {
const roomRef = useRef<Room | null>(null)
const [roomName, setRoomName] = useState('demo-room')
const [participantName, setParticipantName] = useState('')
const [role, setRole] = useState<Role>('publisher')
const [connectionState, setConnectionState] = useState<'idle' | 'connecting' | 'connected'>('idle')
const [isSharing, setIsSharing] = useState(false)
const [remoteCount, setRemoteCount] = useState(0)
const [localScreen, setLocalScreen] = useState<ScreenFeed | null>(null)
const [remoteScreens, setRemoteScreens] = useState<ScreenFeed[]>([])
const [error, setError] = useState('')
useEffect(() => {
return () => {
roomRef.current?.disconnect()
}
}, [])
function refreshRemoteCount(room: Room) {
setRemoteCount(room.remoteParticipants.size)
}
async function joinRoom() {
const normalizedRoom = roomName.trim()
const normalizedName = participantName.trim()
if (!/^[A-Za-z0-9_-]{3,64}$/.test(normalizedRoom)) {
setError('房间号只能包含 3–64 个字母、数字、下划线或连字符。')
return
}
if (!normalizedName) {
setError('请输入显示名称。')
return
}
setError('')
setConnectionState('connecting')
try {
const response = await fetch(`/api/rooms/${encodeURIComponent(normalizedRoom)}/token`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ participantName: normalizedName, role }),
})
const payload = (await response.json()) as TokenResponse | { message: string }
if (!response.ok || !('token' in payload)) {
throw new Error('message' in payload ? payload.message : '无法获取房间凭证。')
}
roomRef.current?.disconnect()
setLocalScreen(null)
setRemoteScreens([])
const room = new Room()
room.on(RoomEvent.ParticipantConnected, () => refreshRemoteCount(room))
room.on(RoomEvent.ParticipantDisconnected, () => refreshRemoteCount(room))
room.on(RoomEvent.TrackSubscribed, (track, publication, participant) => {
if (track.kind === Track.Kind.Video && publication.source === Track.Source.ScreenShare) {
setRemoteScreens((screens) => [
...screens.filter((screen) => screen.track.sid !== track.sid),
{ track, label: `${participant.name || participant.identity} 的屏幕` },
])
}
})
room.on(RoomEvent.TrackUnsubscribed, (track) => {
setRemoteScreens((screens) => screens.filter((screen) => screen.track !== track))
})
room.on(RoomEvent.LocalTrackPublished, (publication) => {
if (publication.source === Track.Source.ScreenShare && publication.track) {
setLocalScreen({ track: publication.track, label: '你正在共享的屏幕' })
setIsSharing(true)
}
})
room.on(RoomEvent.LocalTrackUnpublished, (publication) => {
if (publication.source === Track.Source.ScreenShare) {
setLocalScreen(null)
setIsSharing(false)
}
})
room.on(RoomEvent.Disconnected, () => {
setConnectionState('idle')
setIsSharing(false)
setRemoteCount(0)
setLocalScreen(null)
setRemoteScreens([])
})
await room.connect(payload.livekitUrl, payload.token)
roomRef.current = room
refreshRemoteCount(room)
setConnectionState('connected')
} catch (cause) {
setConnectionState('idle')
setError(cause instanceof Error ? cause.message : '连接房间时发生未知错误。')
}
}
async function toggleScreenShare() {
const room = roomRef.current
if (!room) {
return
}
setError('')
try {
await room.localParticipant.setScreenShareEnabled(!isSharing)
} catch (cause) {
setError(cause instanceof Error ? cause.message : '无法切换屏幕共享。')
}
}
function leaveRoom() {
roomRef.current?.disconnect()
roomRef.current = null
setConnectionState('idle')
setIsSharing(false)
setRemoteCount(0)
setLocalScreen(null)
setRemoteScreens([])
}
const connected = connectionState === 'connected'
const canShare = role === 'host' || role === 'publisher'
return (
<main className="app-shell">
<header>
<p className="eyebrow">LIVEKIT · </p>
<h1></h1>
<p className="subtitle"> LiveKitKtor 访</p>
</header>
<section className="join-panel" aria-label="加入房间">
<label>
<input
value={roomName}
onChange={(event) => setRoomName(event.target.value)}
disabled={connected}
maxLength={64}
/>
</label>
<label>
<input
value={participantName}
onChange={(event) => setParticipantName(event.target.value)}
disabled={connected}
maxLength={64}
placeholder="例如:张三"
/>
</label>
<label>
<select value={role} onChange={(event) => setRole(event.target.value as Role)} disabled={connected}>
<option value="host">Host</option>
<option value="publisher">Publisher</option>
<option value="viewer">Viewer</option>
</select>
</label>
{!connected ? (
<button type="button" className="primary" onClick={() => void joinRoom()} disabled={connectionState === 'connecting'}>
{connectionState === 'connecting' ? '正在连接…' : '加入房间'}
</button>
) : (
<button type="button" className="secondary" onClick={leaveRoom}></button>
)}
</section>
<div className="ticks"></div>
{error && <p className="error" role="alert">{error}</p>}
<section id="next-steps">
<div id="docs">
<svg className="icon" role="presentation" aria-hidden="true">
<use href="/icons.svg#documentation-icon"></use>
</svg>
<h2>Documentation</h2>
<p>Your questions, answered</p>
<ul>
<li>
<a href="https://vite.dev/" target="_blank">
<img className="logo" src={viteLogo} alt="" />
Explore Vite
</a>
</li>
<li>
<a href="https://react.dev/" target="_blank">
<img className="button-icon" src={reactLogo} alt="" />
Learn more
</a>
</li>
</ul>
<section className="status-bar" aria-live="polite">
<span className={connected ? 'status online' : 'status'}>{connected ? '已连接' : '未连接'}</span>
<span>{remoteCount}</span>
{connected && (
<button type="button" className="share-button" onClick={() => void toggleScreenShare()} disabled={!canShare}>
{isSharing ? '停止共享屏幕' : '共享屏幕'}
</button>
)}
{connected && !canShare && <span className="hint">Viewer </span>}
</section>
<section className="screens">
<div className="screen-section">
<h2></h2>
<div className="video-stack">
{localScreen ? <ScreenTrack {...localScreen} /> : <p className="empty"></p>}
</div>
</div>
<div id="social">
<svg className="icon" role="presentation" aria-hidden="true">
<use href="/icons.svg#social-icon"></use>
</svg>
<h2>Connect with us</h2>
<p>Join the Vite community</p>
<ul>
<li>
<a href="https://github.com/vitejs/vite" target="_blank">
<svg
className="button-icon"
role="presentation"
aria-hidden="true"
>
<use href="/icons.svg#github-icon"></use>
</svg>
GitHub
</a>
</li>
<li>
<a href="https://chat.vite.dev/" target="_blank">
<svg
className="button-icon"
role="presentation"
aria-hidden="true"
>
<use href="/icons.svg#discord-icon"></use>
</svg>
Discord
</a>
</li>
<li>
<a href="https://x.com/vite_js" target="_blank">
<svg
className="button-icon"
role="presentation"
aria-hidden="true"
>
<use href="/icons.svg#x-icon"></use>
</svg>
X.com
</a>
</li>
<li>
<a href="https://bsky.app/profile/vite.dev" target="_blank">
<svg
className="button-icon"
role="presentation"
aria-hidden="true"
>
<use href="/icons.svg#bluesky-icon"></use>
</svg>
Bluesky
</a>
</li>
</ul>
<div className="screen-section">
<h2></h2>
<div className="video-stack">
{remoteScreens.length === 0
? <p className="empty"></p>
: remoteScreens.map((screen) => (
<ScreenTrack key={screen.track.sid} {...screen} />
))}
</div>
</div>
</section>
<div className="ticks"></div>
<section id="spacer"></section>
</>
<p className="development-note">
JWT
</p>
</main>
)
}
+5 -103
View File
@@ -1,111 +1,13 @@
:root {
--text: #6b6375;
--text-h: #08060d;
--bg: #fff;
--border: #e5e4e7;
--code-bg: #f4f3ec;
--accent: #aa3bff;
--accent-bg: rgba(170, 59, 255, 0.1);
--accent-border: rgba(170, 59, 255, 0.5);
--social-bg: rgba(244, 243, 236, 0.5);
--shadow:
rgba(0, 0, 0, 0.1) 0 10px 15px -3px, rgba(0, 0, 0, 0.05) 0 4px 6px -2px;
--sans: system-ui, 'Segoe UI', Roboto, sans-serif;
--heading: system-ui, 'Segoe UI', Roboto, sans-serif;
--mono: ui-monospace, Consolas, monospace;
font: 18px/145% var(--sans);
letter-spacing: 0.18px;
color-scheme: light dark;
color: var(--text);
background: var(--bg);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
color: #292a40;
background: #f4f4fa;
font-synthesis: none;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
@media (max-width: 1024px) {
font-size: 16px;
}
}
@media (prefers-color-scheme: dark) {
:root {
--text: #9ca3af;
--text-h: #f3f4f6;
--bg: #16171d;
--border: #2e303a;
--code-bg: #1f2028;
--accent: #c084fc;
--accent-bg: rgba(192, 132, 252, 0.15);
--accent-border: rgba(192, 132, 252, 0.5);
--social-bg: rgba(47, 48, 58, 0.5);
--shadow:
rgba(0, 0, 0, 0.4) 0 10px 15px -3px, rgba(0, 0, 0, 0.25) 0 4px 6px -2px;
}
#social .button-icon {
filter: invert(1) brightness(2);
}
}
#root {
width: 1126px;
max-width: 100%;
margin: 0 auto;
text-align: center;
border-inline: 1px solid var(--border);
min-height: 100svh;
display: flex;
flex-direction: column;
box-sizing: border-box;
}
body {
min-width: 320px;
min-height: 100vh;
margin: 0;
}
h1,
h2 {
font-family: var(--heading);
font-weight: 500;
color: var(--text-h);
}
h1 {
font-size: 56px;
letter-spacing: -1.68px;
margin: 32px 0;
@media (max-width: 1024px) {
font-size: 36px;
margin: 20px 0;
}
}
h2 {
font-size: 24px;
line-height: 118%;
letter-spacing: -0.24px;
margin: 0 0 8px;
@media (max-width: 1024px) {
font-size: 20px;
}
}
p {
margin: 0;
}
code,
.counter {
font-family: var(--mono);
display: inline-flex;
border-radius: 4px;
color: var(--text-h);
}
code {
font-size: 15px;
line-height: 135%;
padding: 4px 8px;
background: var(--code-bg);
}