mirror of
https://github.com/EchoSixHIYA/WebSpeak-client-for-TeamSpeak.git
synced 2026-12-05 21:12:49 +08:00
Add v3 batched downlink with tick-aligned timestamps, audio-clock playback and PLC; add adaptive Opus/PCM uplink and one-shot TURN relay retry. Expose frame size, automatic relay retry and Cloudflare ingress assumptions in the admin console, with persistence, i18n, docs and regression tests.
134 lines
8.9 KiB
TypeScript
134 lines
8.9 KiB
TypeScript
/** Payloads stay correlated with their command names across both runtimes. */
|
|
export interface ClientCommandPayloads {
|
|
createInvite: { expiresInMinutes: number; channelPassword?: string; serverPassword?: string };
|
|
switchChannel: { channelId: string; password?: string };
|
|
moveClient: { clientId: number; channelId: string; password?: string };
|
|
sendTextMessage: { message: string; channelId?: string };
|
|
sendServerMessage: { message: string };
|
|
sendPrivateMessage: { clientId: number; message: string; clientUid?: string };
|
|
poke: { clientId: number; message: string };
|
|
setAway: { away: boolean; message?: string };
|
|
setWhisperTargets: { targetIds: number[] };
|
|
setWhisperActive: { active: boolean };
|
|
setMicrophoneMuted: { muted: boolean };
|
|
setAccompanimentActive: { active: boolean };
|
|
setMemberVolume: { clientId: number; volume: number };
|
|
latencyProbe: { sequence: string };
|
|
audioStatsProbe: { sequence: string };
|
|
audioCaps: { downlinkVersion: 3; maxFrameMs: 20 | 40; codecs: ("pcm" | "opus")[] };
|
|
}
|
|
|
|
export type ClientCommandType = keyof ClientCommandPayloads;
|
|
export type ClientCommand = {
|
|
[K in ClientCommandType]: { type: K; requestId?: string; payload: ClientCommandPayloads[K] }
|
|
}[ClientCommandType];
|
|
|
|
export type ClientCommandResult = ClientCommand | { error: { code: string; message: string } };
|
|
|
|
export function parseClientCommand(raw: string): ClientCommandResult {
|
|
let value: unknown;
|
|
try {
|
|
value = JSON.parse(raw);
|
|
} catch {
|
|
return { error: { code: "INVALID_JSON", message: "消息不是有效的 JSON" } };
|
|
}
|
|
if (!isRecord(value) || typeof value.type !== "string") {
|
|
return { error: { code: "INVALID_MESSAGE", message: "消息类型不能为空" } };
|
|
}
|
|
if (value.requestId !== undefined && (typeof value.requestId !== "string" || value.requestId.length > 64)) {
|
|
return { error: { code: "INVALID_REQUEST_ID", message: "请求标识无效" } };
|
|
}
|
|
const supportedTypes = new Set(["createInvite", "switchChannel", "moveClient", "sendTextMessage", "sendServerMessage", "sendPrivateMessage", "poke", "setAway", "setWhisperTargets", "setWhisperActive", "setMicrophoneMuted", "setAccompanimentActive", "setMemberVolume", "latencyProbe", "audioStatsProbe", "audioCaps"]);
|
|
if (!supportedTypes.has(value.type)) {
|
|
return { error: { code: "UNKNOWN_MESSAGE_TYPE", message: "不支持的消息类型" } };
|
|
}
|
|
if (!isRecord(value.payload)) {
|
|
return { error: { code: "INVALID_PAYLOAD", message: "消息参数无效" } };
|
|
}
|
|
if (value.type === "createInvite" && (!Number.isInteger(value.payload.expiresInMinutes) || Number(value.payload.expiresInMinutes) < 1 || Number(value.payload.expiresInMinutes) > 43200 || (value.payload.channelPassword !== undefined && (typeof value.payload.channelPassword !== "string" || value.payload.channelPassword.length > 512)))) {
|
|
return { error: { code: "INVALID_INVITE", message: "Invalid invitation expiry or credentials" } };
|
|
}
|
|
if (value.type === "createInvite" && value.payload.serverPassword !== undefined && (typeof value.payload.serverPassword !== "string" || value.payload.serverPassword.length > 512)) {
|
|
return { error: { code: "INVALID_INVITE", message: "Invalid invitation credentials" } };
|
|
}
|
|
if (value.type === "switchChannel" && (typeof value.payload.channelId !== "string" || !/^\d{1,20}$/.test(value.payload.channelId))) {
|
|
return { error: { code: "INVALID_CHANNEL_ID", message: "频道标识无效" } };
|
|
}
|
|
if (value.type === "switchChannel" && value.payload.password !== undefined && (typeof value.payload.password !== "string" || value.payload.password.length > 512)) {
|
|
return { error: { code: "INVALID_CHANNEL_PASSWORD", message: "频道密码无效" } };
|
|
}
|
|
if (value.type === "moveClient" && (typeof value.payload.clientId !== "number" || !Number.isInteger(value.payload.clientId) || value.payload.clientId <= 0 || value.payload.clientId > 65535)) {
|
|
return { error: { code: "INVALID_CLIENT_ID", message: "成员标识无效" } };
|
|
}
|
|
if (value.type === "moveClient" && (typeof value.payload.channelId !== "string" || !/^\d{1,20}$/.test(value.payload.channelId))) {
|
|
return { error: { code: "INVALID_CHANNEL_ID", message: "频道标识无效" } };
|
|
}
|
|
if (value.type === "moveClient" && value.payload.password !== undefined && (typeof value.payload.password !== "string" || value.payload.password.length > 512)) {
|
|
return { error: { code: "INVALID_CHANNEL_PASSWORD", message: "频道密码无效" } };
|
|
}
|
|
if (value.type === "sendTextMessage" && (typeof value.payload.message !== "string" || value.payload.message.length > 500)) {
|
|
return { error: { code: "INVALID_TEXT_MESSAGE", message: "文字消息无效" } };
|
|
}
|
|
if (value.type === "sendTextMessage" && value.payload.channelId !== undefined && (typeof value.payload.channelId !== "string" || !/^\d{1,20}$/.test(value.payload.channelId))) {
|
|
return { error: { code: "INVALID_CHANNEL_ID", message: "频道标识无效" } };
|
|
}
|
|
if (value.type === "sendPrivateMessage" && value.payload.clientUid !== undefined && (typeof value.payload.clientUid !== "string" || !value.payload.clientUid || value.payload.clientUid.length > 128)) {
|
|
return { error: { code: "INVALID_CLIENT_ID", message: "成员标识无效" } };
|
|
}
|
|
if ((value.type === "sendServerMessage" || value.type === "sendPrivateMessage") && (typeof value.payload.message !== "string" || value.payload.message.length > 500)) {
|
|
return { error: { code: "INVALID_TEXT_MESSAGE", message: "文字消息无效" } };
|
|
}
|
|
if ((value.type === "sendPrivateMessage" || value.type === "poke") && (typeof value.payload.clientId !== "number" || !Number.isInteger(value.payload.clientId) || value.payload.clientId <= 0 || value.payload.clientId > 65535)) {
|
|
return { error: { code: "INVALID_CLIENT_ID", message: "成员标识无效" } };
|
|
}
|
|
if (value.type === "poke" && (typeof value.payload.message !== "string" || value.payload.message.length > 200)) {
|
|
return { error: { code: "INVALID_POKE_MESSAGE", message: "戳一戳消息无效" } };
|
|
}
|
|
if (value.type === "setAway" && (typeof value.payload.away !== "boolean" || (value.payload.message !== undefined && (typeof value.payload.message !== "string" || value.payload.message.length > 200)))) {
|
|
return { error: { code: "INVALID_AWAY_STATUS", message: "离开状态无效" } };
|
|
}
|
|
if (value.type === "setWhisperTargets") {
|
|
const targetIds = value.payload.targetIds;
|
|
if (!Array.isArray(targetIds) || targetIds.length > 8 || targetIds.some((clientId) => typeof clientId !== "number" || !Number.isInteger(clientId) || clientId <= 0 || clientId > 65535) || new Set(targetIds).size !== targetIds.length) {
|
|
return { error: { code: "INVALID_WHISPER_TARGETS", message: "私语目标无效" } };
|
|
}
|
|
}
|
|
if (value.type === "setWhisperActive" && typeof value.payload.active !== "boolean") {
|
|
return { error: { code: "INVALID_WHISPER_STATE", message: "私语状态无效" } };
|
|
}
|
|
if (value.type === "setMicrophoneMuted" && typeof value.payload.muted !== "boolean") {
|
|
return { error: { code: "INVALID_MICROPHONE_STATE", message: "麦克风状态无效" } };
|
|
}
|
|
if (value.type === "setAccompanimentActive" && typeof value.payload.active !== "boolean") {
|
|
return { error: { code: "INVALID_ACCOMPANIMENT_STATE", message: "伴奏状态无效" } };
|
|
}
|
|
if (value.type === "setMemberVolume" && (typeof value.payload.clientId !== "number" || !Number.isInteger(value.payload.clientId) || value.payload.clientId <= 0 || value.payload.clientId > 65535 || typeof value.payload.volume !== "number" || !Number.isFinite(value.payload.volume) || value.payload.volume < 0 || value.payload.volume > 4)) {
|
|
return { error: { code: "INVALID_MEMBER_VOLUME", message: "成员音量无效" } };
|
|
}
|
|
if (value.type === "audioStatsProbe" && (typeof value.payload.sequence !== "string" || value.payload.sequence.length > 64)) {
|
|
return { error: { code: "INVALID_AUDIO_STATS_PROBE", message: "音频状态采样标识无效" } };
|
|
}
|
|
if (value.type === "latencyProbe" && (typeof value.payload.sequence !== "string" || value.payload.sequence.length > 64)) {
|
|
return { error: { code: "INVALID_LATENCY_PROBE", message: "延迟探测标识无效" } };
|
|
}
|
|
if (value.type === "audioCaps") {
|
|
const payload = value.payload;
|
|
const codecs = payload.codecs;
|
|
if (payload.downlinkVersion !== 3 || (payload.maxFrameMs !== 20 && payload.maxFrameMs !== 40)
|
|
|| !Array.isArray(codecs) || codecs.length < 1 || codecs.length > 2
|
|
|| codecs.some(codec => codec !== "pcm" && codec !== "opus")
|
|
|| new Set(codecs).size !== codecs.length) {
|
|
return { error: { code: "INVALID_AUDIO_CAPS", message: "音频能力声明无效" } };
|
|
}
|
|
}
|
|
return {
|
|
type: value.type as ClientCommand["type"],
|
|
...(typeof value.requestId === "string" ? { requestId: value.requestId } : {}),
|
|
payload: value.payload,
|
|
} as ClientCommand;
|
|
}
|
|
|
|
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
}
|