From 02882871b92c3a3dae76f364d31f1ffc4145a97f Mon Sep 17 00:00:00 2001 From: minie4 Date: Thu, 29 Aug 2024 01:54:10 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Properly=20disconnect=20from=20p?= =?UTF-8?q?revious=20websocket=20on=20server=20change?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/main.dart | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/main.dart b/lib/main.dart index 810454f..0e98bd0 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -63,6 +63,10 @@ class _GoMixHomeState extends State { ValueNotifier connectionError = ValueNotifier(""); void initWs() { + if (wsConnected) { + ws?.sink.close(1000, "disconnect before initWs"); + } + setState(() { connectionError.value = ""; wsConnected = false; @@ -142,6 +146,7 @@ class _GoMixHomeState extends State { wsConnected = false; if (connectionError.value != "") return; Future.delayed(const Duration(seconds: 2), () { + if (wsConnected) return; initWs(); }); }, onError: (err) { @@ -221,7 +226,9 @@ class _GoMixHomeState extends State { icon: const Icon(Icons.remove_circle_outline_outlined), color: Colors.red, tooltip: 'Not connected', - onPressed: () {}, + onPressed: () { + initWs(); + }, ), actions: [ Padding(