Communitygithub.com

evalix098/tiktoklive

点对点实时多人游戏,通过WebRTC数据通道实现全网格连接,服务器仅处理握手协商,实现最低延迟和零消息费用。

tiktoklive 是什么?

这是一个基于WebRTC的点对点(P2P)实时多人游戏技能,适用于2-8人的协作或休闲游戏场景。它利用WebRTC数据通道在浏览器间建立全网格(full mesh)连接,所有游戏流量直接在对等方之间传输,无需经过服务器,从而实现5-40毫秒的超低延迟。服务器仅负责通过`/api/rtc`端点处理连接握手(SDP/ICE)的转发。核心原语`P2PRoom`是无框架的,提供`broadcast()`用于不可靠的连续状态同步(如位置、光标)和`send()`用于可靠的一次性事件。该技能包含详细的信任模型说明,建议仅用于合作和休闲游戏,因为缺乏服务器权威性,容易受到作弊行为影响。同时提供了实际的房间容量限制(约8个对等方)以及处理NAT穿透失败(约10-20%的对等方对)的建议。支持React绑定和诊断功能,如连接状态、RTT和候选类型。

兼容平台~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 毫秒),且无每次刻度的服务器成本。

组件路径
网格原语(从这里开始)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 与框架无关,“房间”只是一个 rendezvous 键——大厅代码、一对一的通话 ID、共享文档 ID 等任意字符串(≤64 个字符)。任何架构都基于以下三个相同的调用:

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); // 不可靠的“状态”通道——游戏速率,陈旧数据丢弃
p2p.send(event, to);    // 可靠通道——恰好一次事件(to 可选)
p2p.close();

对于“此应用上的所有人都一起玩”的常见 React 形式,请从 references/react-binding.md 复制 useP2PRoom 钩子(以及一个经过实际使用的组件:游戏速率广播循环约 20 发送/秒,可靠的一次性事件)。

模式:

  1. broadcast() = 不可靠/无序,用于持续刷新的状态(位置、光标)。send() = 可靠/有序,用于必须恰好到达一次的事件。绝不要将游戏速率的状态流式传输到 send() 上;在广播之间进行插值以实现平滑运动。
  2. 后期加入者一无所知:当 p2p.peers 中出现新对等方时,现有对等方应 send() 当前共享状态。必须恰好有一个对等方响应:比较已经在房间中的对等方(您的 selfId 加上 p2p.peers 减去新加入者)的 ID,并且仅当您的 selfId 是最小时才响应——这样两个同时加入者既不会重复响应也不会得不到响应。
  3. 房间 ID:省略时表示“此应用上的所有人都一起玩”;传递 room: code 用于私人大厅(生成短代码,放入 URL 中)。
  4. 对等方无故消失(标签页关闭、休眠):将 p2p.peers 中缺失的对等方视为已离开,并丢弃其实体。
  5. 一个在首次渲染时捕获 room/name 的 React 绑定(如 references/react-binding.md 中所示)需要重新挂载才能更改它们——请将组件键设为房间代码。

诊断

p2p.peers 中的每个条目都携带 connectionState、rttMs(数据通道 ping)和 candidateType(host/srflx = 直接)。要覆盖 STUN,请将 VITE_STUN_URLS(逗号分隔)添加到 .grok/app-env.json,然后重新启动开发服务器(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).

相关技能