Wait for websocket to be disconnected before continuing

This commit is contained in:
Marco 2024-05-20 17:21:25 +02:00
parent adf8c86692
commit 2a2e219c80
4 changed files with 102 additions and 76 deletions

View File

@ -59,10 +59,11 @@ class ServerConnection {
broadcast.listen(handleIncomingData); broadcast.listen(handleIncomingData);
} }
void disconnectExistingConnection() { Future disconnectExistingConnection() async {
if (channel == null) return; if (channel == null) return;
channel!.sink.close(); await channel!.sink.close();
channel = null; channel = null;
broadcast = const Stream.empty(); broadcast = const Stream.empty();
} }

View File

@ -19,8 +19,8 @@ class ConnectionCubit extends Cubit<ConnectionCubitState> {
ServerConnection.getInstance().connect(playerID, passphrase); ServerConnection.getInstance().connect(playerID, passphrase);
} }
void disonnect() { Future disonnect() async {
ServerConnection.getInstance().disconnectExistingConnection(); return ServerConnection.getInstance().disconnectExistingConnection();
} }
void opponentConnected() { void opponentConnected() {

View File

@ -24,13 +24,16 @@ class CreateGameWidget extends StatefulWidget {
class _CreateGameWidgetState extends State<CreateGameWidget> { class _CreateGameWidgetState extends State<CreateGameWidget> {
late Future<GameInfo?> registerResponse; late Future<GameInfo?> registerResponse;
late Future disconnectFuture;
late ChessGameArguments chessGameArgs; late ChessGameArguments chessGameArgs;
@override @override
void initState() { void initState() {
super.initState(); super.initState();
ConnectionCubit().disonnect(); disconnectFuture = ConnectionCubit().disonnect();
disconnectFuture.then((val) {
registerResponse = createPrivateGame(); registerResponse = createPrivateGame();
});
} }
@override @override
@ -50,7 +53,13 @@ class _CreateGameWidgetState extends State<CreateGameWidget> {
return Scaffold( return Scaffold(
floatingActionButton: fltnBtn, floatingActionButton: fltnBtn,
body: Center( body: Center(
child: FutureBuilder<GameInfo?>( child: FutureBuilder(
future: disconnectFuture,
builder: (context, snapshot) {
if (snapshot.connectionState != ConnectionState.done) {
return Container();
} else {
return FutureBuilder<GameInfo?>(
future: registerResponse, future: registerResponse,
builder: (context, snapshot) { builder: (context, snapshot) {
if (snapshot.connectionState != ConnectionState.done) { if (snapshot.connectionState != ConnectionState.done) {
@ -60,14 +69,16 @@ class _CreateGameWidgetState extends State<CreateGameWidget> {
child: CircularProgressIndicator(), child: CircularProgressIndicator(),
); );
} else { } else {
var passphrase = snapshot.data?.passphrase ?? "no passphrase"; var passphrase =
snapshot.data?.passphrase ?? "no passphrase";
ConnectionCubit().connect( ConnectionCubit().connect(
snapshot.data!.playerID.toString(), snapshot.data!.playerID.toString(),
snapshot.data!.passphrase, snapshot.data!.passphrase,
); );
return BlocListener<ConnectionCubit, ConnectionCubitState>( return BlocListener<ConnectionCubit,
ConnectionCubitState>(
listener: (context, state) { listener: (context, state) {
// We wait for our opponent to connect // We wait for our opponent to connect
if (state.opponentConnected) { if (state.opponentConnected) {
@ -91,7 +102,8 @@ class _CreateGameWidgetState extends State<CreateGameWidget> {
children: [ children: [
SelectableText( SelectableText(
passphrase, passphrase,
style: const TextStyle(fontWeight: FontWeight.bold), style: const TextStyle(
fontWeight: FontWeight.bold),
), ),
IconButton( IconButton(
icon: const Icon(Icons.copy), icon: const Icon(Icons.copy),
@ -109,7 +121,9 @@ class _CreateGameWidgetState extends State<CreateGameWidget> {
); );
} }
}, },
), );
}
}),
), ),
); );
} }

View File

@ -19,16 +19,25 @@ class JoinGameHandleWidget extends StatefulWidget {
class _JoinGameHandleWidgetState extends State<JoinGameHandleWidget> { class _JoinGameHandleWidgetState extends State<JoinGameHandleWidget> {
late Future<GameInfo?> joinGameFuture; late Future<GameInfo?> joinGameFuture;
late Future disconnectFuture;
@override @override
void initState() { void initState() {
super.initState(); super.initState();
ConnectionCubit().disonnect(); disconnectFuture = ConnectionCubit().disonnect();
disconnectFuture.then((val) {
joinGameFuture = joinPrivateGame(widget.passphrase); joinGameFuture = joinPrivateGame(widget.passphrase);
});
} }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return FutureBuilder(
future: disconnectFuture,
builder: (context, snapshot) {
if (snapshot.connectionState != ConnectionState.done) {
return Container();
} else {
return FutureBuilder( return FutureBuilder(
future: joinGameFuture, future: joinGameFuture,
builder: (context, snapshot) { builder: (context, snapshot) {
@ -47,6 +56,8 @@ class _JoinGameHandleWidgetState extends State<JoinGameHandleWidget> {
} }
}); });
} }
});
}
Future<GameInfo?> joinPrivateGame(String phrase) async { Future<GameInfo?> joinPrivateGame(String phrase) async {
http.Response response; http.Response response;