mirror of
https://github.com/EchoSixHIYA/WebSpeak-client-for-TeamSpeak.git
synced 2026-12-03 12:02:50 +08:00
M003: add TeamSpeak reconnect recovery
This commit is contained in:
1 parent
8c7a940de5
commit
3cca211ae9
10 files changed
+362
-98
No files matched your search
+96
-58
@@ -2,6 +2,8 @@ import { EventEmitter } from "node:events";
|
||||
import {
|
||||
Client as TS3FullClient,
|
||||
clientMove as tsClientMove,
|
||||
generateIdentity,
|
||||
type Identity,
|
||||
type VoiceData,
|
||||
type ChannelInfo,
|
||||
type ClientInfo,
|
||||
@@ -16,6 +18,7 @@ export interface TSClientOptions {
|
||||
serverPassword?: string;
|
||||
defaultChannel?: string;
|
||||
channelPassword?: string;
|
||||
identity?: Identity;
|
||||
}
|
||||
|
||||
export interface TSVoiceData {
|
||||
@@ -33,79 +36,42 @@ export class TSClient extends EventEmitter {
|
||||
private client: TS3FullClient | null = null;
|
||||
private adapter: TeamSpeakAdapter | null = null;
|
||||
private logger: Logger;
|
||||
private readonly identity: Identity;
|
||||
private clientId = 0;
|
||||
private connected = false;
|
||||
private preferredChannelId = 0n;
|
||||
|
||||
constructor(private options: TSClientOptions, logger: Logger) {
|
||||
super();
|
||||
this.logger = logger.child({ nickname: options.nickname });
|
||||
this.identity = options.identity ?? generateIdentity(8);
|
||||
}
|
||||
|
||||
async connect(): Promise<void> {
|
||||
this.adapter = new TeamSpeakAdapter({
|
||||
target: this.options.target,
|
||||
nickname: this.options.nickname,
|
||||
serverPassword: this.options.serverPassword,
|
||||
defaultChannel: this.options.defaultChannel,
|
||||
channelPassword: this.options.channelPassword,
|
||||
}, this.logger);
|
||||
this.client = this.adapter.client;
|
||||
if (!this.adapter || !this.client) {
|
||||
this.adapter = new TeamSpeakAdapter({
|
||||
target: this.options.target,
|
||||
nickname: this.options.nickname,
|
||||
identity: this.identity,
|
||||
serverPassword: this.options.serverPassword,
|
||||
defaultChannel: this.options.defaultChannel,
|
||||
channelPassword: this.options.channelPassword,
|
||||
}, this.logger);
|
||||
this.client = this.adapter.client;
|
||||
this.attachClientListeners(this.client);
|
||||
}
|
||||
const adapter = this.adapter;
|
||||
const client = this.client;
|
||||
|
||||
this.client.on("voiceData", (data: VoiceData) => {
|
||||
this.emit("voiceData", {
|
||||
clientId: data.clientId,
|
||||
codec: data.codec,
|
||||
data: Buffer.from(data.data),
|
||||
} as TSVoiceData);
|
||||
});
|
||||
await adapter.connect();
|
||||
|
||||
// The SDK patch exposes the directory that TeamSpeak sends during the
|
||||
// normal client welcome sequence. This is the same data a native client
|
||||
// receives and does not require channellist/clientlist permissions.
|
||||
(this.client as unknown as {
|
||||
on(event: "directorySnapshot", handler: (snapshot: TSDirectorySnapshot) => void): unknown;
|
||||
}).on("directorySnapshot", (snapshot) => {
|
||||
this.emit("directorySnapshot", snapshot);
|
||||
});
|
||||
|
||||
this.client.on("textMessage", (msg) => {
|
||||
this.emit("textMessage", {
|
||||
invokerName: msg.invokerName,
|
||||
invokerId: msg.invokerID,
|
||||
invokerUid: msg.invokerUID,
|
||||
message: msg.message,
|
||||
targetMode: msg.targetMode,
|
||||
});
|
||||
});
|
||||
|
||||
this.client.on("disconnected", (err) => {
|
||||
this.logger.warn({ err: err?.message }, "Disconnected from TS");
|
||||
this.connected = false;
|
||||
this.clientId = 0;
|
||||
this.emit("disconnected");
|
||||
});
|
||||
|
||||
this.client.on("clientEnter", (info) => {
|
||||
this.emit("clientEnter", info);
|
||||
});
|
||||
|
||||
this.client.on("clientLeave", (info) => {
|
||||
this.emit("clientLeave", info);
|
||||
});
|
||||
|
||||
this.client.on("clientMoved", (info) => {
|
||||
this.emit("clientMoved", info);
|
||||
});
|
||||
|
||||
await this.adapter.connect();
|
||||
|
||||
this.clientId = this.client.clientID();
|
||||
this.clientId = client.clientID();
|
||||
try {
|
||||
// A native TeamSpeak client subscribes to the complete channel tree
|
||||
// after the welcome sequence. Without this command the server only
|
||||
// exposes members in the current channel, so users disappear as soon as
|
||||
// they move elsewhere even though both clients are on the same server.
|
||||
await this.client.execCommand("channelsubscribeall", 5000);
|
||||
await client.execCommand("channelsubscribeall", 5000);
|
||||
} catch (error: unknown) {
|
||||
this.logger.warn({
|
||||
err: error instanceof Error ? error.message : String(error),
|
||||
@@ -116,12 +82,79 @@ export class TSClient extends EventEmitter {
|
||||
// contains members from every subscribed channel.
|
||||
await new Promise<void>((resolve) => setImmediate(resolve));
|
||||
await new Promise<void>((resolve) => setImmediate(resolve));
|
||||
|
||||
const connectedChannelId = client.channelID();
|
||||
if (this.preferredChannelId === 0n) {
|
||||
this.preferredChannelId = connectedChannelId;
|
||||
} else if (connectedChannelId !== this.preferredChannelId) {
|
||||
try {
|
||||
await tsClientMove(client, this.clientId, this.preferredChannelId);
|
||||
} catch (error: unknown) {
|
||||
// The old channel may have been deleted or become inaccessible. The
|
||||
// SDK has already left us in the server/default channel, which is the
|
||||
// safe fallback required by the reconnect contract.
|
||||
this.logger.info({
|
||||
channelId: this.preferredChannelId.toString(),
|
||||
err: error instanceof Error ? error.message : String(error),
|
||||
}, "Previous TeamSpeak channel could not be restored");
|
||||
this.preferredChannelId = client.channelID();
|
||||
}
|
||||
}
|
||||
this.connected = true;
|
||||
|
||||
this.logger.info({ clientId: this.clientId }, "Connected to TeamSpeak");
|
||||
this.emit("connected", this.clientId);
|
||||
}
|
||||
|
||||
private attachClientListeners(client: TS3FullClient): void {
|
||||
client.on("voiceData", (data: VoiceData) => {
|
||||
this.emit("voiceData", {
|
||||
clientId: data.clientId,
|
||||
codec: data.codec,
|
||||
data: Buffer.from(data.data),
|
||||
} as TSVoiceData);
|
||||
});
|
||||
|
||||
// The SDK patch exposes the directory that TeamSpeak sends during the
|
||||
// normal client welcome sequence. This is the same data a native client
|
||||
// receives and does not require channellist/clientlist permissions.
|
||||
(client as unknown as {
|
||||
on(event: "directorySnapshot", handler: (snapshot: TSDirectorySnapshot) => void): unknown;
|
||||
}).on("directorySnapshot", (snapshot) => {
|
||||
this.emit("directorySnapshot", snapshot);
|
||||
});
|
||||
|
||||
client.on("textMessage", (msg) => {
|
||||
this.emit("textMessage", {
|
||||
invokerName: msg.invokerName,
|
||||
invokerId: msg.invokerID,
|
||||
invokerUid: msg.invokerUID,
|
||||
message: msg.message,
|
||||
targetMode: msg.targetMode,
|
||||
});
|
||||
});
|
||||
|
||||
client.on("disconnected", (err) => {
|
||||
this.logger.warn({ err: err?.message }, "Disconnected from TS");
|
||||
this.connected = false;
|
||||
this.clientId = 0;
|
||||
this.emit("disconnected", err);
|
||||
});
|
||||
|
||||
client.on("clientEnter", (info) => {
|
||||
this.emit("clientEnter", info);
|
||||
});
|
||||
|
||||
client.on("clientLeave", (info) => {
|
||||
this.emit("clientLeave", info);
|
||||
});
|
||||
|
||||
client.on("clientMoved", (info) => {
|
||||
if (info.id === this.clientId && info.targetChannelID !== 0n) this.preferredChannelId = info.targetChannelID;
|
||||
this.emit("clientMoved", info);
|
||||
});
|
||||
}
|
||||
|
||||
sendVoice(data: Buffer, codec: number = 4): void {
|
||||
if (!this.client || !this.connected) return;
|
||||
this.client.sendVoice(data, codec);
|
||||
@@ -139,8 +172,9 @@ export class TSClient extends EventEmitter {
|
||||
}
|
||||
|
||||
async switchChannel(channelId: bigint, password?: string): Promise<void> {
|
||||
if (!this.client) return;
|
||||
if (!this.client || !this.connected) return;
|
||||
await tsClientMove(this.client, this.clientId, channelId, password);
|
||||
this.preferredChannelId = channelId;
|
||||
}
|
||||
|
||||
getClientId(): number {
|
||||
@@ -154,6 +188,10 @@ export class TSClient extends EventEmitter {
|
||||
return this.adapter?.protocol ?? null;
|
||||
}
|
||||
|
||||
getIdentityString(): string {
|
||||
return this.identity.toString();
|
||||
}
|
||||
|
||||
getChannelId(): bigint {
|
||||
if (!this.client) return 0n;
|
||||
const sdkClient = this.client as unknown as { channelID?: () => bigint };
|
||||
|
||||
Reference in new issue
Block a user