From 3185438f36eff8ed8e4ddc583cd0fe42f84631fe Mon Sep 17 00:00:00 2001 From: hailin Date: Wed, 25 Feb 2026 03:37:16 -0800 Subject: [PATCH] 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 --- it0_app/lib/core/network/websocket_client.dart | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/it0_app/lib/core/network/websocket_client.dart b/it0_app/lib/core/network/websocket_client.dart index 5633e8a..efed8bb 100644 --- a/it0_app/lib/core/network/websocket_client.dart +++ b/it0_app/lib/core/network/websocket_client.dart @@ -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(