diff --git a/packages/stage-ui/src/stores/providers/aliyun/stream-transcription.ts b/packages/stage-ui/src/stores/providers/aliyun/stream-transcription.ts index 8792d57cc..e1759570e 100644 --- a/packages/stage-ui/src/stores/providers/aliyun/stream-transcription.ts +++ b/packages/stage-ui/src/stores/providers/aliyun/stream-transcription.ts @@ -179,17 +179,24 @@ async function startRealtimeSession(options: InternalRealtimeOptions): Promise await reader.cancel()) - if (websocket) { - if (websocket.readyState === WebSocket.OPEN) { + if (websocket && closeSocket) { + switch (websocket.readyState) { + case WebSocket.OPEN: + if (sendStop) mayThrow(() => session.stop(websocket)) websocket.close(1000, 'client closed') - } - else { - mayThrow(() => websocket?.close()) + break + case WebSocket.CONNECTING: + websocket.close(1000, 'client closed') + break + default: + // If the server has already initiated closure, avoid sending another close frame. + break } } @@ -235,7 +242,7 @@ async function startRealtimeSession(options: InternalRealtimeOptions): Promise