From 75abe72bf64c24a97aec8d607cef9d7261818f9c Mon Sep 17 00:00:00 2001 From: imbytecat Date: Sun, 1 Mar 2026 04:21:57 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=81=9C=E6=AD=A2?= =?UTF-8?q?=E5=BD=95=E9=9F=B3=E5=90=8E=E6=9C=AA=E7=AD=89=E5=BE=85=E8=B1=86?= =?UTF-8?q?=E5=8C=85=E8=BF=94=E5=9B=9E=E6=9C=80=E7=BB=88=E7=BB=93=E6=9E=9C?= =?UTF-8?q?=EF=BC=8C=E5=AF=BC=E8=87=B4=E5=8E=86=E5=8F=B2=E8=AE=B0=E5=BD=95?= =?UTF-8?q?=E5=92=8C=E8=87=AA=E5=8A=A8=E7=B2=98=E8=B4=B4=E5=A4=B1=E6=95=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/asr/client.go | 24 +++++++++++++++++++----- main.go | 4 +--- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/internal/asr/client.go b/internal/asr/client.go index e6030c8..baf763c 100644 --- a/internal/asr/client.go +++ b/internal/asr/client.go @@ -111,6 +111,7 @@ func (c *Client) SendAudio(pcm []byte, last bool) error { // readLoop reads server responses and forwards them to resultCh. func (c *Client) readLoop(resultCh chan<- wsMsg.ServerMsg) { defer func() { + c.conn.Close() c.mu.Lock() c.closed = true c.mu.Unlock() @@ -153,13 +154,26 @@ func (c *Client) readLoop(resultCh chan<- wsMsg.ServerMsg) { } } } -// Close shuts down the ASR connection. -func (c *Client) Close() { +// Finish sends the last audio frame and waits for ASR to return final results. +func (c *Client) Finish() { c.mu.Lock() - if !c.closed { - c.conn.Close() + if c.closed { + c.mu.Unlock() + return } 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 } \ No newline at end of file diff --git a/main.go b/main.go index e073c2a..71ff015 100644 --- a/main.go +++ b/main.go @@ -120,9 +120,7 @@ func main() { } } cleanup := func() { - // Send last empty frame to signal end - _ = client.SendAudio(nil, true) - client.Close() + client.Finish() } return sendAudio, cleanup, nil }