🐛 Properly disconnect from previous websocket on server change

This commit is contained in:
2024-08-29 01:54:10 +02:00
parent a973ccf509
commit 02882871b9

View File

@ -63,6 +63,10 @@ class _GoMixHomeState extends State<GoMixHome> {
ValueNotifier<String> 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<GoMixHome> {
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<GoMixHome> {
icon: const Icon(Icons.remove_circle_outline_outlined),
color: Colors.red,
tooltip: 'Not connected',
onPressed: () {},
onPressed: () {
initWs();
},
),
actions: <Widget>[
Padding(