mirror of
https://github.com/EchoSixHIYA/WebSpeak-client-for-TeamSpeak.git
synced 2026-12-03 03:52:50 +08:00
feat: add real TeamSpeak whisper routing
This commit is contained in:
1 parent
cab7707e51
commit
96ef77c789
7 files changed
+306
-14
No files matched your search
@@ -176,6 +176,19 @@ export class TSClient extends EventEmitter {
|
||||
this.client.sendVoice(data, codec);
|
||||
}
|
||||
|
||||
sendWhisper(data: Buffer, clientIds: number[], codec: number = 4): void {
|
||||
if (!this.client || !this.connected) return;
|
||||
const targets = [...new Set(clientIds)].filter((clientId) => Number.isInteger(clientId) && clientId > 0 && clientId <= 65535 && clientId !== this.getClientId());
|
||||
if (!targets.length) throw new Error("Whisper requires at least one valid target");
|
||||
const whisperClient = this.client as unknown as {
|
||||
sendWhisper?: (voiceData: Uint8Array, targetClientIds: number[], voiceCodec: number) => void;
|
||||
};
|
||||
if (typeof whisperClient.sendWhisper !== "function") {
|
||||
throw new Error("TeamSpeak SDK whisper support is unavailable");
|
||||
}
|
||||
whisperClient.sendWhisper(data, targets, codec);
|
||||
}
|
||||
|
||||
async sendTextMessage(scope: TSChatScope, message: string, targetId = 0n): Promise<void> {
|
||||
if (!this.client || !this.connected) throw new Error("TeamSpeak session is not ready");
|
||||
const targetMode = scope === "private" ? 1 : scope === "server" ? 3 : 2;
|
||||
|
||||
@@ -54,6 +54,8 @@ interface WebClientEntry {
|
||||
members: Map<number, ChannelMember>;
|
||||
eventLog: ServerEvent[];
|
||||
opusEncoder: { encode(pcm: Buffer): Buffer } | null;
|
||||
whisperTargetIds: Set<number>;
|
||||
whisperActive: boolean;
|
||||
isAlive: boolean;
|
||||
reconnectTimer: ReturnType<typeof setTimeout> | null;
|
||||
}
|
||||
@@ -126,6 +128,8 @@ export class VoiceBridge {
|
||||
members: new Map(),
|
||||
eventLog: [],
|
||||
opusEncoder: null,
|
||||
whisperTargetIds: new Set(),
|
||||
whisperActive: false,
|
||||
isAlive: true,
|
||||
reconnectTimer: null,
|
||||
};
|
||||
@@ -166,6 +170,7 @@ export class VoiceBridge {
|
||||
const refreshDirectory = () => {
|
||||
const snapshot = directory.getSnapshot();
|
||||
if (!snapshot) return;
|
||||
const previousWhisperTargets = [...entry!.whisperTargetIds].sort((a, b) => a - b);
|
||||
const effectiveSelfId = selfId || tsClient.getClientId();
|
||||
const sdkChannelId = tsClient.getChannelId();
|
||||
if (selfChannelId === 0n && sdkChannelId !== 0n) selfChannelId = sdkChannelId;
|
||||
@@ -184,6 +189,14 @@ export class VoiceBridge {
|
||||
channelCommander: client.channelCommander,
|
||||
});
|
||||
}
|
||||
for (const clientId of entry!.whisperTargetIds) {
|
||||
if (!entry!.members.has(clientId) || clientId === effectiveSelfId) entry!.whisperTargetIds.delete(clientId);
|
||||
}
|
||||
if (!entry!.whisperTargetIds.size) entry!.whisperActive = false;
|
||||
const nextWhisperTargets = [...entry!.whisperTargetIds].sort((a, b) => a - b);
|
||||
if (initialStateSent && (previousWhisperTargets.length !== nextWhisperTargets.length || previousWhisperTargets.some((clientId, index) => clientId !== nextWhisperTargets[index]))) {
|
||||
sendJson({ type: "whisperTargets", targetIds: nextWhisperTargets, active: entry!.whisperActive });
|
||||
}
|
||||
};
|
||||
const trackChannelEvents = (previous: unknown[], next: unknown[]) => {
|
||||
if (!initialStateSent) return;
|
||||
@@ -211,6 +224,8 @@ export class VoiceBridge {
|
||||
tsClientId: selfId,
|
||||
members: Array.from(entry!.members.values()),
|
||||
serverEventLog: entry!.eventLog,
|
||||
whisperTargetIds: [...entry!.whisperTargetIds],
|
||||
whisperActive: entry!.whisperActive,
|
||||
...(entry!.rememberIdentity ? { identity: tsClient.getIdentityString() } : {}),
|
||||
});
|
||||
sendJson({ type: "channelList", channels: entry!.channelTree });
|
||||
@@ -225,6 +240,8 @@ export class VoiceBridge {
|
||||
directory.clear();
|
||||
entry!.channelTree = [];
|
||||
entry!.members.clear();
|
||||
entry!.whisperTargetIds.clear();
|
||||
entry!.whisperActive = false;
|
||||
};
|
||||
|
||||
const failReconnect = (normalized: ReturnType<typeof normalizeTeamSpeakError>) => {
|
||||
@@ -407,7 +424,11 @@ export class VoiceBridge {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
if (entry!.opusEncoder) tsClient.sendVoice(entry!.opusEncoder.encode(frame), 4);
|
||||
if (entry!.opusEncoder) {
|
||||
const encoded = entry!.opusEncoder.encode(frame);
|
||||
if (entry!.whisperActive && entry!.whisperTargetIds.size) tsClient.sendWhisper(encoded, [...entry!.whisperTargetIds], 4);
|
||||
else tsClient.sendVoice(encoded, 4);
|
||||
}
|
||||
} catch {
|
||||
// A frame arriving during shutdown is safe to discard.
|
||||
}
|
||||
@@ -491,6 +512,8 @@ export class VoiceBridge {
|
||||
entry.reconnectTimer = null;
|
||||
}
|
||||
entry.opusEncoder = null;
|
||||
entry.whisperTargetIds.clear();
|
||||
entry.whisperActive = false;
|
||||
entry.channelTree = [];
|
||||
entry.members.clear();
|
||||
entry.tsClient.removeAllListeners();
|
||||
@@ -575,6 +598,24 @@ async function handleCommand(
|
||||
await entry.tsClient.poke(clientId, (command.payload.message as string).trim());
|
||||
} else if (command.type === "setAway") {
|
||||
await entry.tsClient.setAway(command.payload.away as boolean, typeof command.payload.message === "string" ? command.payload.message.trim() : "");
|
||||
} else if (command.type === "setWhisperTargets") {
|
||||
const targetIds = command.payload.targetIds as number[];
|
||||
const selfId = entry.tsClient.getClientId();
|
||||
if (targetIds.some((clientId) => clientId === selfId || !entry.members.has(clientId))) {
|
||||
sendJson({ type: "error", requestId: command.requestId, error: { code: "CLIENT_NOT_FOUND", message: "私语目标已离线", recoverable: false } });
|
||||
return;
|
||||
}
|
||||
entry.whisperTargetIds = new Set(targetIds);
|
||||
if (!entry.whisperTargetIds.size) entry.whisperActive = false;
|
||||
sendJson({ type: "whisperTargets", targetIds: [...entry.whisperTargetIds], active: entry.whisperActive });
|
||||
} else if (command.type === "setWhisperActive") {
|
||||
const active = command.payload.active as boolean;
|
||||
if (active && !entry.whisperTargetIds.size) {
|
||||
sendJson({ type: "error", requestId: command.requestId, error: { code: "NO_WHISPER_TARGETS", message: "请先选择私语目标", recoverable: false } });
|
||||
return;
|
||||
}
|
||||
entry.whisperActive = active;
|
||||
sendJson({ type: "whisperTargets", targetIds: [...entry.whisperTargetIds], active: entry.whisperActive });
|
||||
}
|
||||
if (command.requestId) sendJson({ type: "commandCompleted", requestId: command.requestId });
|
||||
} catch (error: unknown) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
export interface ClientCommand {
|
||||
type: "switchChannel" | "sendTextMessage" | "sendServerMessage" | "sendPrivateMessage" | "poke" | "setAway";
|
||||
type: "switchChannel" | "sendTextMessage" | "sendServerMessage" | "sendPrivateMessage" | "poke" | "setAway" | "setWhisperTargets" | "setWhisperActive";
|
||||
requestId?: string;
|
||||
payload: Record<string, unknown>;
|
||||
}
|
||||
@@ -19,7 +19,7 @@ export function parseClientCommand(raw: string): ClientCommandResult {
|
||||
if (value.requestId !== undefined && (typeof value.requestId !== "string" || value.requestId.length > 64)) {
|
||||
return { error: { code: "INVALID_REQUEST_ID", message: "请求标识无效" } };
|
||||
}
|
||||
const supportedTypes = new Set(["switchChannel", "sendTextMessage", "sendServerMessage", "sendPrivateMessage", "poke", "setAway"]);
|
||||
const supportedTypes = new Set(["switchChannel", "sendTextMessage", "sendServerMessage", "sendPrivateMessage", "poke", "setAway", "setWhisperTargets", "setWhisperActive"]);
|
||||
if (!supportedTypes.has(value.type)) {
|
||||
return { error: { code: "UNKNOWN_MESSAGE_TYPE", message: "不支持的消息类型" } };
|
||||
}
|
||||
@@ -47,6 +47,15 @@ export function parseClientCommand(raw: string): ClientCommandResult {
|
||||
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: "私语状态无效" } };
|
||||
}
|
||||
return {
|
||||
type: value.type as ClientCommand["type"],
|
||||
...(typeof value.requestId === "string" ? { requestId: value.requestId } : {}),
|
||||
|
||||
Reference in new issue
Block a user