fix: close previous WebSocket before opening new connection

When sending a second message in the same session, the old WebSocket
connection was not closed, causing both connections to subscribe to the
same session room. This resulted in each text event being received twice,
producing garbled/duplicated output text.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
hailin 2026-02-25 03:37:16 -08:00
parent e02b350043
commit 3185438f36
1 changed files with 12 additions and 0 deletions

View File

@ -30,6 +30,18 @@ class WebSocketClient {
_lastPath = path;
_lastToken = token;
// Close previous connection to prevent duplicate event delivery
if (_channel != null) {
_heartbeatTimer?.cancel();
try {
await _channel!.sink.close();
} catch (_) {
// Ignore errors when closing stale connections
}
_channel = null;
_isConnected = false;
}
final uri = Uri.parse('$baseUrl$path');
try {
_channel = IOWebSocketChannel.connect(