Communitygithub.com

evalix098/tiktoklive

透過 WebRTC 資料通道實現點對點即時多人連線:每個部署應用的用戶直接與其他所有用戶建立連線(全網狀拓撲),伺服器僅在 /api/rtc 處理握手協商。極低延遲、零每訊息伺服器成本。適用於 2-8 人的合作/休閒即時應用:共享游標、繪圖、派對遊戲、休閒動作遊戲。觸發詞:p2p, peer to peer, webrtc, low latency multiplayer, direct connection。

tiktoklive 是什麼?

此技能提供一個基於 WebRTC 點對點(P2P)的全網狀多人連線方案。所有訪問同一部署網域的用戶會自動加入一個預設房間,並與其他每個訪客建立原生的 WebRTC 資料通道,遊戲流量完全繞過伺服器,僅透過 `/api/rtc` 的微型中繼服務處理連線握手(SDP/ICE)。由於是瀏覽器對瀏覽器的直接連線,延遲可低至 5-40 毫秒,且無任何每 tick 的伺服器費用。該方案支援 React 綁定(由使用者自行建立)、可選的信令中繼與 HTTP 路由檔案,並提供 `P2PRoom` 原始物件、框架無關的廣播與可靠傳送 API,以及與即時同步相關的多種模式(如重生遊戲房間 ID、處理遲到加入者、診斷連線狀態等)。它適合合作與休閒遊戲情境,但不支援競爭排名、作弊偵測或匿名陌生人配對——因為每個對等節點都獨立執行規則,且可能互相獲知 IP 位址。實務上限約 8 個對等點,且約 10-20% 的配對因嚴格的 NAT 而無法連線。

相容平台~Claude Code~Codex CLI✓Cursor
npx skills add https://github.com/evalix098/tiktoklive/tree/HEAD/.grok/skills/multiplayer-p2p

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

多人連線(WebRTC 點對點)

所有訪問同一部署網域的訪客會自動加入一個預設房間,並與每個其他訪客建立原生的 WebRTC 資料通道——遊戲流量完全不會觸及伺服器。一個微小的中繼服務位於 /api/rtc,僅處理連線握手(SDP/ICE)的路由,讓對等點之間直接連線。您從此工具包中使用的部分是客戶端專屬的;中繼服務歸您所有。

延遲是瀏覽器↔瀏覽器(通常 5–40 毫秒),且無任何每 tick 的伺服器成本。

元件路徑
網狀原始物件(從這裡開始)P2PRoom 來自 @/lib/multiplayer
React 房間綁定(選用,您自行建立)src/lib/multiplayer/use-p2p-room.ts
信令中繼(您自行建立)src/lib/multiplayer/signaling.server.ts
HTTP 掛載點(您自行建立)src/routes/api/rtc.ts

信任模型——在選擇 P2P 前請務必閱讀。 沒有伺服器權威:每個對等點執行自己版本的規則,並且可以偽造資料(位置、分數等)。對等點在 ICE 過程中還會獲知彼此的 IP 位址。P2P 適用於選擇一起遊玩的人之間的合作與休閒遊戲——絕不適用於競爭排名、作弊敏感性或匿名陌生人的配對。競爭性或作弊敏感的遊玩在此範本中不受支援:請在產品層面拒絕此類需求,而不是在 P2P 上實作。

實務限制:全網狀連線是 O(N²) 條連線——將房間上限設為約 8 個對等點。約有 10–20% 的對等點配對位於嚴格的 NAT 之後而無法連線;工具包會針對每個對等點顯示 connectionState: "failed" ——請在 UI 中顯示此狀態,而非讓應用卡死。

設定(一次性)

建立兩個伺服器端檔案——沒有它們就無法運作:

  1. src/lib/multiplayer/signaling.server.ts —— 基於資料庫的信令中繼(正式部署使用 Neon,預覽環境使用 PGLite)。
  2. src/routes/api/rtc.ts —— 將其掛載到 /api/rtc(GET 輪詢、POST 信令/離開)。

請從 references/signaling-relay.md 複製這兩個檔案,該檔案也包含了模式注意事項:中繼會在首次使用時自動建立自己的兩個資料表(CREATE TABLE IF NOT EXISTS),因此除非您刻意想要擁有該模式,否則請勿在 migrations/ 中加入任何內容。

使用原始物件

P2PRoom 不依賴任何框架,「房間」只是一個會合鍵——可以是大廳代碼、一對一通話 ID、共用文件 ID、任何字串(≤64 個字元)。任何架構都可以建立在以下三個相同的 API 呼叫之上:

import { P2PRoom } from "@/lib/multiplayer";

const p2p = new P2PRoom({
  room: "doc-42",
  selfId: myId,
  name: "ani",
  onPeersChanged: (peers) => render(peers),
  onMessage: (from, data, channel) => apply(from, data, channel),
});
await p2p.join();
p2p.broadcast(state); // 不可靠的 "state" 通道——遊戲頻率的廣播,舊資料會被捨棄
p2p.send(event, to); // 可靠通道——確保一次傳送的事件(to 參數為選用)
p2p.close();

對於 React 中常見的「此應用程式中所有人都一起玩」的情境,請從 references/react-binding.md 複製 useP2PRoom hook(以及一個完整的組件範例:約 20 次/秒的遊戲頻率廣播循環,以及可靠的一次性事件)。

模式:

  1. broadcast() = 不可靠/無序,用於持續更新的狀態(位置、游標)。send() = 可靠/有序,用於必須剛好送達一次的事件。切勿在 send() 上串流遊戲頻率的狀態;應透過在廣播之間插值來實現平滑移動。
  2. 後加入者一無所知:當一個新對等點出現在 p2p.peers 中時,應有一個既有對等點使用 send() 向其傳送當前的共享狀態。必須剛好有一個對等點回應:比較已在房間中的對等點 ID(您的 selfId 加上 p2p.peers 減去新加入者),並僅在您的 selfId 最小時才回應——這樣兩個同時加入者既不會重複回應也不會無人回應。
  3. 房間 ID:若要讓「此應用程式中所有人都一起玩」則省略;若要建立私人房間,則傳入 room: code(產生一個短代碼,放入 URL 中)。
  4. 對等點可能在未告別的情況下消失(分頁關閉、睡眠):將從 p2p.peers 中消失的對等點視為已離開,並將其對應的實體刪除。
  5. 一個在首次渲染時捕獲 room/name 的 React 綁定(例如 references/react-binding.md 中的那個)需要重新掛載才能變更它們——請將組件以房間代碼進行 key 綁定。

診斷

p2p.peers 中的每個條目都帶有 connectionState、rttMs(資料通道 ping)和 candidateType(host/srflx = 直接連線)。若要覆蓋 STUN 伺服器,請在 .grok/app-env.json 中加入 VITE_STUN_URLS(逗號分隔),然後重新啟動開發伺服器(Vite 在啟動時讀取環境變數;HMR 不會重新載入)——請勿在此沙盒中撰寫.env 檔案。

Individual skills in this repo

This repo contains 17 individual skills — each has its own dedicated page.

evalix098/tiktoklive

Add user accounts and sign-in to this TanStack Start app. Use when the app needs authentication, sign-in, user accounts, protected routes, or per-user data. Triggers on "auth", "login", "log in", "sign in", "sign up", "account", "users", "authentication", "protected", "who is logged in", "current user", "per-user".

evalix098/tiktoklive

Build browser games and interactive/canvas/3D experiences in this TanStack Start + React app. Use for any game, simulation, or WebGL/Canvas experience — 2D or 3D, single-player. Covers the game loop & timing, 3D orientation/camera conventions, collision, performance, assets, audio, save, game feel, and per-genre playbooks. For WASD / vehicle / flight **input signs and inverted A/D**, open the **`controls`** skill — do not rely on this file or racing-kart alone. Triggers on "game", "minecraft", "fps", "platformer", "racing", "tetris", "snake", "shooter", "3d", "three.js", "canvas", "voxel", "physics".

evalix098/tiktoklive

Player-facing input signs for browser games: WASD, vehicles, flight, FPS mouse-look, and the #1 failure mode (inverted A/D). Mandatory control self-tests and a tiny test interface so you can verify A turns left before shipping. Load for ANY game with movement, steering, flying, driving, tanks, boats, mechs, drones, planes, karts, third-person follow cams — not only racing. Triggers on "controls", "WASD", "inverted", "steer", "flight", "airplane", "kart", "vehicle", "yaw", "roll", "pitch", "mouse look", "A/D".

evalix098/tiktoklive

Design and build polished, non-generic UI for this TanStack Start + React + Tailwind v4 + shadcn/Radix app. Use whenever you create or restyle any interface surface — pages, landing pages, dashboards, forms, modals, nav, and game overlays (start screens, HUD, menus). Covers design tokens, layout, typography, color, spacing, motion, and the anti-"AI-slop" rules that keep output from looking generic. Triggers on "design", "UI", "make it look good", "polish", "landing page", "theme", "style", "redesign", "ugly", "clean up".

evalix098/tiktoklive

Deep guide for game ANIMATION assets: motion cycles, action keyframes, effect sequences, and animation sprite sheets — built around a video-first pipeline. In this app-builder sandbox, execute via the video2dsprite / generate2dsprite skills (magenta + scripts), not ad-hoc ffmpeg. Use whenever generating anything that moves: walk/run cycles, attacks, idles, FX, flags, fire, animation sheets. Complements game-asset-core.

evalix098/tiktoklive

Core discipline for ANY game-asset generation with Imagine tools: the engine-ready defaults users don't state, spec checklists, style anchoring, read-back verification, honest defect flagging. Use whenever generating any game art (sprites, sheets, animations, tiles, UI, FX) — then ALSO load the matching specialist skill: game-animation-frames for anything that moves, game-tilesets for tiles/terrain, game-character-consistency for recurring characters, game-ui-icons for UI and icons.

evalix098/tiktoklive

Deep guide for CHARACTER IDENTITY across images: turnarounds (front/side/ back), state and damage variants, palette swaps, equipment changes, and same-character-in-context sets. Use whenever generating character turnarounds, character sheets, variants of an existing sprite, or any same-subject multi-image set. Complements game-asset-core.

evalix098/tiktoklive

Deep guide for game TILE assets: seamless tileable textures, terrain transition tilesets, autotiles, and ground/platform tiles. Use whenever generating tileable textures, tilesets, terrain transitions, or seamless patterns. Complements game-asset-core.

evalix098/tiktoklive

Deep guide for game UI assets: buttons with interaction states, panels, bars, wordmark logos, and icon sets. Use whenever generating game UI elements, HUD assets, inventory icons, icon sets, buttons, or title logos. Complements game-asset-core.

evalix098/tiktoklive

Generate production-oriented 2D game maps with `imagine_text_to_image`: RPG/top-down maps, side-scroller parallax stages, tilemaps, layered raster maps, prop packs, collision zones, and walkable areas. Use when building browser games that need real map art (not pure code-drawn tiles), layered props, or map collision metadata. Triggers on "map", "level", "stage", "tilemap", "overworld", "dungeon", "side scroller background", "prop pack", "2D map".

evalix098/tiktoklive

Generate and postprocess 2D game sprites and animation sheets: pixel-art characters, NPCs, creatures, spells, projectiles, impacts, props, summons, and transparent PNG/GIF exports. Use when building browser games that need real sprite sheets (not code-drawn placeholders), matching a map art style, or producing magenta-background sheets for chroma-key cleanup. Triggers on "sprite", "sprite sheet", "animation sheet", "pixel art character", "walk cycle", "attack animation", "projectile sprite", "2D game asset".

evalix098/tiktoklive

How to use the Imagine tools in Grok Build: imagine_text_to_image, imagine_image_to_image, imagine_reference_to_image, imagine_text_to_video, imagine_image_to_video, imagine_reference_to_video, and render_file for chat previews. When to build a visual with code instead of generating it, prompt-craft, reference-first handling of real people, factual grounding, and asset-consistency. Load this whenever generating or editing an image or video is on the table. Tool-usage-driven, not triggered by a user merely mentioning images.

evalix098/tiktoklive

Use Neon Postgres (the database) in this TanStack Start app. Use when the app needs to store or query data, persist state, or keep per-user data. Triggers on "database", "Postgres", "Neon", "save data", "store data", "persist", "tables", "SQL", "query", "migrations".

evalix098/tiktoklive

Share-link previews and app identity for apps on *.grok.me: the injector-owned og:image card, the SVG favicon, and PWA icons for installable apps. Use when scaffolding, renaming, or restyling the app — and for share / unfurl / OG / Twitter card questions. A custom 1200×630 card from the app's own art is the default — games of every kind (DOM board/word games included), whimsical apps, creative tools, and brand-forward pages; only plain utilities keep the placeholder. Always run the brand-asset pass as a `task` subagent and never wait for it. Triggers on "share", "rename", "app name", "OG", "Open Graph", "twitter card", "unfurl", "og:image", "og:type", "x:game:image", "x-banner", "link preview", "social card", "thumbnail", "preview image", "favicon", "app icon", "PWA", "manifest", "installable", "home screen", "SEO", "meta description".

evalix098/tiktoklive

Official Three.js API and TSL (Three.js Shading Language) reference for LLM code generation. Load when writing or debugging three.js / WebGL / WebGPU / custom materials / shaders / GLTF / advanced three APIs beyond basic game loop/controls. Prefer building-games for game correctness (loop, WASD, camera, orientation); use this skill for full API/TSL depth. Triggers on "three.js", "threejs", "WebGPU", "TSL", "NodeMaterial", "shader", "GLTF", "MeshStandard", "OrbitControls", "WebGLRenderer".

evalix098/tiktoklive

Grok Build only. Turn a 2D character still into denser animation sprites via imagine_text_to_image base → imagine_image_to_video (6s/10s run-in-place) → ffmpeg frames → magenta chroma-key → dense sampled strips/grids/GIFs. Use when the user wants video-to-sprite, smoother run/walk cycles, or denser intermediate poses. Prefer generate2dsprite for crisp production pixel sheets. Triggers on "video to sprite", "imagine_image_to_video sprite", "dense walk cycle", "smooth run animation from video".

evalix098/tiktoklive

Call the xAI API (Grok) from this app's server code using the injected XAI_API_KEY: chat/LLM features, image and video generation (Imagine), and voice (text-to-speech). Use when the app needs any "AI" / "assistant" / "chatbot" / "Grok" functionality, runtime image/video generation, or speech. Triggers on "AI", "LLM", "chatbot", "assistant", "Grok", "xAI", "generate text", "summarize", "generate image", "AI video", "voice", "text to speech", "TTS", "OpenAI" (use xAI instead).

相關技能