SDK 参考

加载 SDK

运行时(OBS)从运行中的应用加载;开发/参考时从本站加载。

<!-- runtime (OBS): running app -->
<script src="http://localhost:3030/sdk/castdeck-sdk.js"></script>

<!-- development / reference -->
<script src="https://castdeck.studio/developers/sdk/castdeck-sdk.js"></script>

API

API
setup(options?)options = { port?: number = 3030, level?: "public" | "rich" }. Call before connect().
connect()Opens the comment stream (SSE /comments/events). Auto-reconnects every 3s.
disconnect()Closes the stream.
subscribe(cb)cb(ev: ProjectedEvent) is called for every event. Returns an unsubscribe function.
subscribeKind(kind, cb)kind = "comment" | "gift" | "membership" | "follow" | "stats". Returns an unsubscribe function.
onSnapshot(cb)cb(events[]) with the recent history on connect (up to 50). Returns an unsubscribe function.
getTierColor(tier)Maps a gift tier (blue/teal/green/yellow/orange/magenta/red) to a CSS color string.

事件数据(ProjectedEvent)

public 为默认。rich(金额、头像等)需要主播同意;未同意时会自动降级为 public。

FieldLevel
kindpublic"comment" | "gift" | "membership" | "follow" | "stats"
platformpublic"youtube" | "twitch" | "twitcasting" | "kick" | "nicolive" | "whowatch"
idpublicStable per-platform id.
timestamppublicEpoch milliseconds.
namepublicDisplay name of the author.
messagepublicComment text.
isMember / isModerator / isOwnerpublicAuthor role flags.
anonymouspublicTrue for anonymous posters (Niconico 184 / Whowatch).
tierpublicGift color tier.
gift.displayText / gift.tier / gift.itemNamepublicGift display info.
gift.amount / gift.currency / gift.jpyEstimaterichNumeric gift amounts (require viewer consent).
membershipEvent / monthspublic / richMembership event type / duration.
userIdrichPrefixed viewer id.
avatarUrlrichAuthor avatar URL.
memberMonthsrichMembership months.
statspublicViewer / like counters for stats events.

castdeck-sdk.d.ts — TypeScript type definitions (exact API & event types)