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 }