M003: add TeamSpeak reconnect recovery

This commit is contained in:
illusia committed 2026-09-01 16:59:48 +08:00
1 parent 8c7a940de5
commit 3cca211ae9
10 files changed
+362 -98

No files matched your search

+68 -6
View File
@@ -3,6 +3,9 @@ import { reactive, ref } from "vue";
export interface VoiceState {
connected: boolean;
connecting: boolean;
reconnecting: boolean;
reconnectAttempt: number;
reconnectFailed: boolean;
tsClientId: number;
error: string;
}
@@ -37,11 +40,13 @@ export interface ChatMessage {
export function useVoiceWebSocket() {
const ws = ref<WebSocket | null>(null);
const state = reactive<VoiceState>({ connected: false, connecting: false, tsClientId: 0, error: "" });
const state = reactive<VoiceState>({ connected: false, connecting: false, reconnecting: false, reconnectAttempt: 0, reconnectFailed: false, tsClientId: 0, error: "" });
const members = reactive<ChannelMember[]>([]);
const channels = reactive<ChannelInfo[]>([]);
const chatMessages = reactive<ChatMessage[]>([]);
let connectionSequence = 0;
let lastConnection: { target: string; channel: string; nickname: string; serverPassword: string } | null = null;
let microphoneStartPromise: Promise<void> | null = null;
// Audio capture. The ScriptProcessor path remains the most compatible option
// for the current browser support matrix, but the graph now has an explicit
@@ -199,6 +204,16 @@ export function useVoiceWebSocket() {
await refreshInputDevices();
}
async function ensureMicrophone(): Promise<void> {
if (micStream) return;
if (!microphoneStartPromise) {
microphoneStartPromise = startMicrophone().finally(() => {
microphoneStartPromise = null;
});
}
await microphoneStartPromise;
}
function voxGate(samples: Float32Array): boolean {
let sum = 0;
const count = Math.min(256, samples.length);
@@ -313,10 +328,14 @@ export function useVoiceWebSocket() {
}
function connect(target: string, channel: string, nickname: string, serverPassword = ""): void {
disconnect();
disconnect(true);
lastConnection = { target, channel, nickname, serverPassword };
const sequence = ++connectionSequence;
state.error = "";
state.connecting = true;
state.reconnecting = false;
state.reconnectAttempt = 0;
state.reconnectFailed = false;
void openTicketedConnection(sequence, target, channel, nickname, serverPassword);
}
@@ -350,7 +369,7 @@ export function useVoiceWebSocket() {
socket.close(1000);
return;
}
startMicrophone().catch((error: unknown) => {
ensureMicrophone().catch((error: unknown) => {
state.error = `麦克风访问失败:${error instanceof Error ? error.message : "请检查浏览器权限"}`;
});
};
@@ -369,7 +388,8 @@ export function useVoiceWebSocket() {
if (sequence !== connectionSequence) return;
state.connected = false;
state.connecting = false;
if (event.code !== 1000 && !state.error) state.error = closeReason(event.code);
state.reconnecting = false;
if (event.code !== 1000 && !state.error && !state.reconnectFailed) state.error = closeReason(event.code);
stopMicrophone();
};
socket.onerror = () => {
@@ -392,14 +412,18 @@ export function useVoiceWebSocket() {
return "连接已断开";
}
function disconnect(): void {
function disconnect(preserveConnection = false): void {
connectionSequence++;
if (!preserveConnection) lastConnection = null;
stopMicrophone();
const socket = ws.value;
ws.value = null;
if (socket && socket.readyState < WebSocket.CLOSING) socket.close(1000);
state.connected = false;
state.connecting = false;
state.reconnecting = false;
state.reconnectAttempt = 0;
state.reconnectFailed = false;
state.tsClientId = 0;
members.length = 0;
channels.length = 0;
@@ -417,8 +441,13 @@ export function useVoiceWebSocket() {
function handleMessage(msg: any): void {
switch (msg.type) {
case "connected":
const wasReconnecting = state.reconnecting;
state.connected = true;
state.connecting = false;
state.reconnecting = false;
state.reconnectAttempt = 0;
state.reconnectFailed = false;
state.error = "";
state.tsClientId = Number(msg.tsClientId) || 0;
if (Array.isArray(msg.members)) {
members.length = 0;
@@ -426,6 +455,11 @@ export function useVoiceWebSocket() {
members.push({ ...member, isSelf: Number(member.id) === state.tsClientId });
}
}
if (wasReconnecting) {
ensureMicrophone().catch((error: unknown) => {
state.error = `麦克风访问失败:${error instanceof Error ? error.message : "请检查浏览器权限"}`;
});
}
break;
case "memberEnter":
if (!members.some((member) => member.id === msg.id)) {
@@ -458,7 +492,29 @@ export function useVoiceWebSocket() {
case "disconnected":
state.connected = false;
state.connecting = false;
state.error = "TeamSpeak 连接已断开";
state.reconnecting = Boolean(msg.recoverable !== false);
state.reconnectFailed = false;
if (!state.reconnecting) state.error = "TeamSpeak 连接已断开";
stopMicrophone();
break;
case "reconnecting":
state.connected = false;
state.connecting = false;
state.reconnecting = true;
state.reconnectFailed = false;
state.reconnectAttempt = Number(msg.attempt) || state.reconnectAttempt + 1;
stopMicrophone();
break;
case "reconnected":
state.reconnecting = false;
state.reconnectFailed = false;
break;
case "reconnectFailed":
state.connected = false;
state.connecting = false;
state.reconnecting = false;
state.reconnectFailed = true;
state.error = "连接已中断,无法自动恢复";
break;
case "error":
state.error = protocolErrorMessage(String(msg.error?.code || ""), String(msg.error?.message || msg.message || "操作失败"));
@@ -495,6 +551,11 @@ export function useVoiceWebSocket() {
});
}
function reconnectNow(): void {
if (!lastConnection || state.connecting) return;
connect(lastConnection.target, lastConnection.channel, lastConnection.nickname, lastConnection.serverPassword);
}
function setMicMode(mode: "vox" | "ptt"): void {
micMode.value = mode;
if (mode === "vox") pttPressed = false;
@@ -564,6 +625,7 @@ export function useVoiceWebSocket() {
startMicrophoneTest,
stopMicrophoneTest,
connect,
reconnectNow,
disconnect,
switchChannel,
sendTextMessage,
+25 -1
View File
@@ -1,7 +1,7 @@
<template>
<div class="web-client" @keydown="onKeyDown" @keyup="onKeyUp" tabindex="0">
<!-- Connection / welcome screen -->
<section v-if="!voiceState.connected" class="join-page">
<section v-if="!voiceState.connected && !voiceState.reconnecting && !voiceState.reconnectFailed" class="join-page">
<header class="join-header">
<div class="brand-lockup">
<div class="brand-mark"><Icon name="waveform" :size="22" /></div>
@@ -92,6 +92,11 @@
</div>
</header>
<div v-if="voiceState.reconnecting || voiceState.reconnectFailed" :class="['reconnect-banner', { failed: voiceState.reconnectFailed }]" role="status">
<div class="reconnect-copy"><strong>{{ voiceState.reconnectFailed ? t('reconnectFailed') : t('connectionInterrupted') }}</strong><span v-if="voiceState.reconnecting">{{ t('reconnectingAttempt', { attempt: voiceState.reconnectAttempt }) }}</span><span v-else>{{ localizedMessage(voiceState.error) }}</span></div>
<div class="reconnect-actions"><button v-if="voiceState.reconnectFailed" type="button" class="secondary-button" @click="reconnectNow">{{ t('reconnectNow') }}</button><button type="button" class="text-button" @click="doDisconnect">{{ t('back') }}</button></div>
</div>
<div class="workspace-scroll">
<div class="workspace-content">
<section class="room-hero">
@@ -207,6 +212,7 @@ const {
startMicrophoneTest,
stopMicrophoneTest,
connect,
reconnectNow,
disconnect,
switchChannel,
sendTextMessage,
@@ -342,6 +348,11 @@ const translations: Record<Language, Record<string, string>> = {
volumeTip: "拖动成员右侧滑杆,单独调整听到的音量。",
moreMemberOptions: "更多成员选项",
connectedToast: "当前已连接到此服务器",
connectionInterrupted: "连接已中断,正在尝试恢复…",
reconnectingAttempt: "第 {{attempt}} 次重连",
reconnectFailed: "无法恢复连接",
reconnectNow: "立即重连",
back: "返回",
volumeToast: "成员音量可以在列表中单独调整",
copiedToast: "邀请链接已复制",
copyFailedToast: "复制失败,请手动复制浏览器地址",
@@ -478,6 +489,11 @@ const translations: Record<Language, Record<string, string>> = {
volumeTip: "Drag a member slider to adjust their volume just for you.",
moreMemberOptions: "More member options",
connectedToast: "You are connected to this server",
connectionInterrupted: "Connection interrupted",
reconnectingAttempt: "Reconnecting… Attempt {{attempt}}",
reconnectFailed: "Could not restore the connection",
reconnectNow: "Reconnect now",
back: "Back",
volumeToast: "Adjust each member's volume from the list",
copiedToast: "Invite link copied",
copyFailedToast: "Copy failed. Copy the browser address manually",
@@ -982,6 +998,14 @@ function stopPointerTalk() {
.settings-select:disabled { cursor: wait; opacity: .65; }
.settings-error { margin: -9px 0 15px; color: #b14e47; font-size: 10px; line-height: 1.5; }
.settings-footer { justify-content: flex-end; }
.reconnect-banner { display: flex; align-items: center; justify-content: space-between; gap: 18px; margin: 14px auto 0; width: min(950px, calc(100% - 64px)); padding: 12px 16px; color: #6c5a2c; border: 1px solid #f0dfae; border-radius: 10px; background: #fff9e8; }
.reconnect-banner.failed { color: #8f4540; border-color: #f2d1cd; background: #fff2f1; }
.reconnect-copy { display: flex; align-items: baseline; gap: 10px; min-width: 0; }
.reconnect-copy strong { font-size: 13px; }
.reconnect-copy span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 11px; }
.reconnect-actions { display: flex; align-items: center; gap: 12px; flex: 0 0 auto; }
.reconnect-actions .secondary-button { min-height: 34px; padding-inline: 13px; }
@media (max-width: 740px) { .reconnect-banner { align-items: flex-start; flex-direction: column; gap: 10px; width: calc(100% - 30px); }.reconnect-copy { align-items: flex-start; flex-direction: column; gap: 4px; }.reconnect-actions { width: 100%; justify-content: flex-end; } }
.save-button { min-height: 38px; }
/* Increase connected-view typography by 25% while keeping the layout compact. */