Wait for websocket to be disconnected before continuing
This commit is contained in:
parent
adf8c86692
commit
2a2e219c80
@ -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();
|
||||
}
|
||||
|
@ -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() {
|
||||
|
@ -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();
|
||||
disconnectFuture = ConnectionCubit().disonnect();
|
||||
disconnectFuture.then((val) {
|
||||
registerResponse = createPrivateGame();
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
@ -50,7 +53,13 @@ class _CreateGameWidgetState extends State<CreateGameWidget> {
|
||||
return Scaffold(
|
||||
floatingActionButton: fltnBtn,
|
||||
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,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState != ConnectionState.done) {
|
||||
@ -60,14 +69,16 @@ class _CreateGameWidgetState extends State<CreateGameWidget> {
|
||||
child: CircularProgressIndicator(),
|
||||
);
|
||||
} else {
|
||||
var passphrase = snapshot.data?.passphrase ?? "no passphrase";
|
||||
var passphrase =
|
||||
snapshot.data?.passphrase ?? "no passphrase";
|
||||
|
||||
ConnectionCubit().connect(
|
||||
snapshot.data!.playerID.toString(),
|
||||
snapshot.data!.passphrase,
|
||||
);
|
||||
|
||||
return BlocListener<ConnectionCubit, ConnectionCubitState>(
|
||||
return BlocListener<ConnectionCubit,
|
||||
ConnectionCubitState>(
|
||||
listener: (context, state) {
|
||||
// We wait for our opponent to connect
|
||||
if (state.opponentConnected) {
|
||||
@ -91,7 +102,8 @@ class _CreateGameWidgetState extends State<CreateGameWidget> {
|
||||
children: [
|
||||
SelectableText(
|
||||
passphrase,
|
||||
style: const TextStyle(fontWeight: FontWeight.bold),
|
||||
style: const TextStyle(
|
||||
fontWeight: FontWeight.bold),
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.copy),
|
||||
@ -109,7 +121,9 @@ class _CreateGameWidgetState extends State<CreateGameWidget> {
|
||||
);
|
||||
}
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
@ -19,16 +19,25 @@ class JoinGameHandleWidget extends StatefulWidget {
|
||||
|
||||
class _JoinGameHandleWidgetState extends State<JoinGameHandleWidget> {
|
||||
late Future<GameInfo?> joinGameFuture;
|
||||
late Future disconnectFuture;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
ConnectionCubit().disonnect();
|
||||
disconnectFuture = ConnectionCubit().disonnect();
|
||||
disconnectFuture.then((val) {
|
||||
joinGameFuture = joinPrivateGame(widget.passphrase);
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return FutureBuilder(
|
||||
future: disconnectFuture,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState != ConnectionState.done) {
|
||||
return Container();
|
||||
} else {
|
||||
return FutureBuilder(
|
||||
future: joinGameFuture,
|
||||
builder: (context, snapshot) {
|
||||
@ -47,6 +56,8 @@ class _JoinGameHandleWidgetState extends State<JoinGameHandleWidget> {
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Future<GameInfo?> joinPrivateGame(String phrase) async {
|
||||
http.Response response;
|
||||
|
Loading…
Reference in New Issue
Block a user