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);
}
void disconnectExistingConnection() {
Future disconnectExistingConnection() async {
if (channel == null) return;
channel!.sink.close();
await channel!.sink.close();
channel = null;
broadcast = const Stream.empty();
}

View File

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

View File

@ -24,13 +24,16 @@ class CreateGameWidget extends StatefulWidget {
class _CreateGameWidgetState extends State<CreateGameWidget> {
late Future<GameInfo?> 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<CreateGameWidget> {
return Scaffold(
floatingActionButton: fltnBtn,
body: Center(
child: FutureBuilder<GameInfo?>(
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<GameInfo?>(
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<ConnectionCubit, ConnectionCubitState>(
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<ConnectionCubit,
ConnectionCubitState>(
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()
],
),
);
}
},
),
);
}
},
);
}
}),
),
);
}

View File

@ -19,31 +19,42 @@ class JoinGameHandleWidget extends StatefulWidget {
class _JoinGameHandleWidgetState extends State<JoinGameHandleWidget> {
late Future<GameInfo?> 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();
}
});
}
});
}