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);
|
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();
|
||||||
}
|
}
|
||||||
|
@ -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() {
|
||||||
|
@ -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();
|
||||||
registerResponse = createPrivateGame();
|
disconnectFuture.then((val) {
|
||||||
|
registerResponse = createPrivateGame();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@ -50,66 +53,77 @@ class _CreateGameWidgetState extends State<CreateGameWidget> {
|
|||||||
return Scaffold(
|
return Scaffold(
|
||||||
floatingActionButton: fltnBtn,
|
floatingActionButton: fltnBtn,
|
||||||
body: Center(
|
body: Center(
|
||||||
child: FutureBuilder<GameInfo?>(
|
child: FutureBuilder(
|
||||||
future: registerResponse,
|
future: disconnectFuture,
|
||||||
builder: (context, snapshot) {
|
builder: (context, snapshot) {
|
||||||
if (snapshot.connectionState != ConnectionState.done) {
|
if (snapshot.connectionState != ConnectionState.done) {
|
||||||
return const SizedBox(
|
return Container();
|
||||||
height: 100,
|
} else {
|
||||||
width: 100,
|
return FutureBuilder<GameInfo?>(
|
||||||
child: CircularProgressIndicator(),
|
future: registerResponse,
|
||||||
);
|
builder: (context, snapshot) {
|
||||||
} else {
|
if (snapshot.connectionState != ConnectionState.done) {
|
||||||
var passphrase = snapshot.data?.passphrase ?? "no passphrase";
|
return const SizedBox(
|
||||||
|
height: 100,
|
||||||
|
width: 100,
|
||||||
|
child: CircularProgressIndicator(),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
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,
|
||||||
listener: (context, state) {
|
ConnectionCubitState>(
|
||||||
// We wait for our opponent to connect
|
listener: (context, state) {
|
||||||
if (state.opponentConnected) {
|
// We wait for our opponent to connect
|
||||||
//TODO: is goNamed the correct way to navigate?
|
if (state.opponentConnected) {
|
||||||
context.goNamed('game', pathParameters: {
|
//TODO: is goNamed the correct way to navigate?
|
||||||
'phrase': passphrase.toURL(),
|
context.goNamed('game', pathParameters: {
|
||||||
});
|
'phrase': passphrase.toURL(),
|
||||||
}
|
});
|
||||||
},
|
}
|
||||||
child: Column(
|
},
|
||||||
mainAxisAlignment: MainAxisAlignment.center,
|
child: Column(
|
||||||
children: [
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
Text(
|
children: [
|
||||||
'Give this phrase to your friend and sit tight:',
|
Text(
|
||||||
style: TextStyle(
|
'Give this phrase to your friend and sit tight:',
|
||||||
color: Theme.of(context).colorScheme.primary),
|
style: TextStyle(
|
||||||
),
|
color: Theme.of(context).colorScheme.primary),
|
||||||
const SizedBox(height: 25),
|
),
|
||||||
Row(
|
const SizedBox(height: 25),
|
||||||
mainAxisAlignment: MainAxisAlignment.center,
|
Row(
|
||||||
children: [
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
SelectableText(
|
children: [
|
||||||
passphrase,
|
SelectableText(
|
||||||
style: const TextStyle(fontWeight: FontWeight.bold),
|
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()
|
|
||||||
],
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -19,31 +19,42 @@ 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();
|
||||||
joinGameFuture = joinPrivateGame(widget.passphrase);
|
disconnectFuture.then((val) {
|
||||||
|
joinGameFuture = joinPrivateGame(widget.passphrase);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return FutureBuilder(
|
return FutureBuilder(
|
||||||
future: joinGameFuture,
|
future: disconnectFuture,
|
||||||
builder: (context, snapshot) {
|
builder: (context, snapshot) {
|
||||||
if (snapshot.connectionState != ConnectionState.done) {
|
if (snapshot.connectionState != ConnectionState.done) {
|
||||||
return const SizedBox(
|
return Container();
|
||||||
height: 100,
|
|
||||||
width: 100,
|
|
||||||
child: CircularProgressIndicator(),
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
ConnectionCubit.getInstance().connect(
|
return FutureBuilder(
|
||||||
snapshot.data!.playerID!.uuid,
|
future: joinGameFuture,
|
||||||
snapshot.data!.passphrase,
|
builder: (context, snapshot) {
|
||||||
);
|
if (snapshot.connectionState != ConnectionState.done) {
|
||||||
return const ChessGame();
|
return const SizedBox(
|
||||||
|
height: 100,
|
||||||
|
width: 100,
|
||||||
|
child: CircularProgressIndicator(),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
ConnectionCubit.getInstance().connect(
|
||||||
|
snapshot.data!.playerID!.uuid,
|
||||||
|
snapshot.data!.passphrase,
|
||||||
|
);
|
||||||
|
return const ChessGame();
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user