fix: 修复停止录音后未等待豆包返回最终结果,导致历史记录和自动粘贴失效

This commit is contained in:
2026-03-01 04:21:57 +08:00
parent e1c1b2d9b2
commit 75abe72bf6
2 changed files with 20 additions and 8 deletions

View File

@@ -111,6 +111,7 @@ func (c *Client) SendAudio(pcm []byte, last bool) error {
// readLoop reads server responses and forwards them to resultCh. // readLoop reads server responses and forwards them to resultCh.
func (c *Client) readLoop(resultCh chan<- wsMsg.ServerMsg) { func (c *Client) readLoop(resultCh chan<- wsMsg.ServerMsg) {
defer func() { defer func() {
c.conn.Close()
c.mu.Lock() c.mu.Lock()
c.closed = true c.closed = true
c.mu.Unlock() c.mu.Unlock()
@@ -153,13 +154,26 @@ func (c *Client) readLoop(resultCh chan<- wsMsg.ServerMsg) {
} }
} }
} }
// Close shuts down the ASR connection. // Finish sends the last audio frame and waits for ASR to return final results.
func (c *Client) Close() { func (c *Client) Finish() {
c.mu.Lock() c.mu.Lock()
if !c.closed { if c.closed {
c.conn.Close() c.mu.Unlock()
return
} }
c.mu.Unlock() c.mu.Unlock()
// Wait for readLoop to finish _ = c.SendAudio(nil, true)
<-c.closeCh
}
// Close forcefully shuts down the ASR connection.
func (c *Client) Close() {
c.mu.Lock()
if c.closed {
c.mu.Unlock()
return
}
c.conn.Close()
c.mu.Unlock()
<-c.closeCh <-c.closeCh
} }

View File

@@ -120,9 +120,7 @@ func main() {
} }
} }
cleanup := func() { cleanup := func() {
// Send last empty frame to signal end client.Finish()
_ = client.SendAudio(nil, true)
client.Close()
} }
return sendAudio, cleanup, nil return sendAudio, cleanup, nil
} }