From 2a2e219c80c88be574b07845d7a76f9a24317995 Mon Sep 17 00:00:00 2001 From: Marco Date: Mon, 20 May 2024 17:21:25 +0200 Subject: [PATCH] Wait for websocket to be disconnected before continuing --- lib/connection/ws_connection.dart | 5 +- lib/connection_cubit/connection_cubit.dart | 4 +- lib/pages/create_game_widget.dart | 132 ++++++++++++--------- lib/pages/join_game_handle_widget.dart | 37 ++++-- 4 files changed, 102 insertions(+), 76 deletions(-) diff --git a/lib/connection/ws_connection.dart b/lib/connection/ws_connection.dart index 85dcd99..0e5f10c 100644 --- a/lib/connection/ws_connection.dart +++ b/lib/connection/ws_connection.dart @@ -59,10 +59,11 @@ class ServerConnection { broadcast.listen(handleIncomingData); } - void disconnectExistingConnection() { + Future disconnectExistingConnection() async { if (channel == null) return; - channel!.sink.close(); + await channel!.sink.close(); + channel = null; broadcast = const Stream.empty(); } diff --git a/lib/connection_cubit/connection_cubit.dart b/lib/connection_cubit/connection_cubit.dart index 3ef0c5e..2f877b8 100644 --- a/lib/connection_cubit/connection_cubit.dart +++ b/lib/connection_cubit/connection_cubit.dart @@ -19,8 +19,8 @@ class ConnectionCubit extends Cubit { ServerConnection.getInstance().connect(playerID, passphrase); } - void disonnect() { - ServerConnection.getInstance().disconnectExistingConnection(); + Future disonnect() async { + return ServerConnection.getInstance().disconnectExistingConnection(); } void opponentConnected() { diff --git a/lib/pages/create_game_widget.dart b/lib/pages/create_game_widget.dart index d005bba..e31f723 100644 --- a/lib/pages/create_game_widget.dart +++ b/lib/pages/create_game_widget.dart @@ -24,13 +24,16 @@ class CreateGameWidget extends StatefulWidget { class _CreateGameWidgetState extends State { late Future registerResponse; + late Future disconnectFuture; late ChessGameArguments chessGameArgs; @override void initState() { super.initState(); - ConnectionCubit().disonnect(); - registerResponse = createPrivateGame(); + disconnectFuture = ConnectionCubit().disonnect(); + disconnectFuture.then((val) { + registerResponse = createPrivateGame(); + }); } @override @@ -50,66 +53,77 @@ class _CreateGameWidgetState extends State { return Scaffold( floatingActionButton: fltnBtn, body: Center( - child: FutureBuilder( - future: registerResponse, - builder: (context, snapshot) { - if (snapshot.connectionState != ConnectionState.done) { - return const SizedBox( - height: 100, - width: 100, - child: CircularProgressIndicator(), - ); - } else { - var passphrase = snapshot.data?.passphrase ?? "no passphrase"; + child: FutureBuilder( + future: disconnectFuture, + builder: (context, snapshot) { + if (snapshot.connectionState != ConnectionState.done) { + return Container(); + } else { + return FutureBuilder( + future: registerResponse, + builder: (context, snapshot) { + if (snapshot.connectionState != ConnectionState.done) { + return const SizedBox( + height: 100, + width: 100, + child: CircularProgressIndicator(), + ); + } else { + var passphrase = + snapshot.data?.passphrase ?? "no passphrase"; - ConnectionCubit().connect( - snapshot.data!.playerID.toString(), - snapshot.data!.passphrase, - ); + ConnectionCubit().connect( + snapshot.data!.playerID.toString(), + snapshot.data!.passphrase, + ); - return BlocListener( - listener: (context, state) { - // We wait for our opponent to connect - if (state.opponentConnected) { - //TODO: is goNamed the correct way to navigate? - context.goNamed('game', pathParameters: { - 'phrase': passphrase.toURL(), - }); - } - }, - child: Column( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Text( - 'Give this phrase to your friend and sit tight:', - style: TextStyle( - color: Theme.of(context).colorScheme.primary), - ), - const SizedBox(height: 25), - Row( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - SelectableText( - passphrase, - style: const TextStyle(fontWeight: FontWeight.bold), + return BlocListener( + listener: (context, state) { + // We wait for our opponent to connect + if (state.opponentConnected) { + //TODO: is goNamed the correct way to navigate? + context.goNamed('game', pathParameters: { + 'phrase': passphrase.toURL(), + }); + } + }, + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Text( + 'Give this phrase to your friend and sit tight:', + style: TextStyle( + color: Theme.of(context).colorScheme.primary), + ), + const SizedBox(height: 25), + Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + SelectableText( + passphrase, + style: const TextStyle( + fontWeight: FontWeight.bold), + ), + IconButton( + icon: const Icon(Icons.copy), + onPressed: () async { + await Clipboard.setData( + ClipboardData(text: passphrase)); + }, + ) + ], + ), + const SizedBox(height: 25), + const CircularProgressIndicator() + ], ), - IconButton( - icon: const Icon(Icons.copy), - onPressed: () async { - await Clipboard.setData( - ClipboardData(text: passphrase)); - }, - ) - ], - ), - const SizedBox(height: 25), - const CircularProgressIndicator() - ], - ), - ); - } - }, - ), + ); + } + }, + ); + } + }), ), ); } diff --git a/lib/pages/join_game_handle_widget.dart b/lib/pages/join_game_handle_widget.dart index 9c0dfbc..28f3c66 100644 --- a/lib/pages/join_game_handle_widget.dart +++ b/lib/pages/join_game_handle_widget.dart @@ -19,31 +19,42 @@ class JoinGameHandleWidget extends StatefulWidget { class _JoinGameHandleWidgetState extends State { late Future joinGameFuture; + late Future disconnectFuture; @override void initState() { super.initState(); - ConnectionCubit().disonnect(); - joinGameFuture = joinPrivateGame(widget.passphrase); + disconnectFuture = ConnectionCubit().disonnect(); + disconnectFuture.then((val) { + joinGameFuture = joinPrivateGame(widget.passphrase); + }); } @override Widget build(BuildContext context) { return FutureBuilder( - future: joinGameFuture, + future: disconnectFuture, builder: (context, snapshot) { if (snapshot.connectionState != ConnectionState.done) { - return const SizedBox( - height: 100, - width: 100, - child: CircularProgressIndicator(), - ); + return Container(); } else { - ConnectionCubit.getInstance().connect( - snapshot.data!.playerID!.uuid, - snapshot.data!.passphrase, - ); - return const ChessGame(); + return FutureBuilder( + future: joinGameFuture, + builder: (context, snapshot) { + if (snapshot.connectionState != ConnectionState.done) { + return const SizedBox( + height: 100, + width: 100, + child: CircularProgressIndicator(), + ); + } else { + ConnectionCubit.getInstance().connect( + snapshot.data!.playerID!.uuid, + snapshot.data!.passphrase, + ); + return const ChessGame(); + } + }); } }); }