Change connection handling (Use playerID from registering to connect via websocket.

This commit is contained in:
Marco 2023-05-28 17:44:22 +02:00
parent 299b77d249
commit b0d6f4002c
10 changed files with 66 additions and 26 deletions

View File

@ -31,7 +31,7 @@ class ServerConnection {
counter++;
}
void connect() {
void connect(String playerID) {
if (wasConnected) channel.sink.close();
if (kDebugMode) {
@ -41,6 +41,7 @@ class ServerConnection {
channel =
WebSocketChannel.connect(Uri.parse('wss://chess.sw-gross.de:8080'));
}
send(playerID);
log(channel.closeCode.toString());

View File

@ -15,8 +15,8 @@ class ConnectionCubit extends Cubit<ConnectionCubitState> {
return _instance;
}
void connect() {
ServerConnection.getInstance().connect();
void connect(String playerID) {
ServerConnection.getInstance().connect(playerID);
emit(ConnectionCubitState(true));
}
}

17
lib/models/models.dart Normal file
View File

@ -0,0 +1,17 @@
import 'package:uuid/uuid.dart';
class ResponseFromRegisteringGame {
final UuidValue playerID;
const ResponseFromRegisteringGame({
required this.playerID,
});
factory ResponseFromRegisteringGame.fromJson(Map<String, dynamic> json) {
final uuid = UuidValue(json['playerID']);
return ResponseFromRegisteringGame(
playerID: uuid,
);
}
}

View File

@ -1,6 +1,7 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';
import 'package:mchess/chess_bloc/chess_bloc.dart';
import 'package:mchess/chess/chess_board.dart';
@ -9,9 +10,11 @@ import 'package:mchess/connection_cubit/connection_cubit.dart';
import 'package:mchess/connection/ws_connection.dart';
import 'package:mchess/utils/widgets/server_log_widget.dart';
import 'package:uuid/uuid.dart';
class ChessGame extends StatefulWidget {
const ChessGame({super.key});
final UuidValue playerID;
const ChessGame({required this.playerID, super.key});
@override
State<ChessGame> createState() => _ChessGameState();
@ -21,7 +24,7 @@ class _ChessGameState extends State<ChessGame> {
@override
void initState() {
super.initState();
ConnectionCubit.getInstance().connect();
ConnectionCubit.getInstance().connect(widget.playerID.uuid);
}
@override
@ -59,9 +62,9 @@ class _ChessGameState extends State<ChessGame> {
),
floatingActionButton: FloatingActionButton(
onPressed: () {
ConnectionCubit.getInstance().connect();
context.goNamed('lobbySelector');
},
child: const Icon(Icons.network_wifi),
child: const Icon(Icons.arrow_back),
),
);
}

View File

@ -1,8 +1,5 @@
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:http/http.dart' as http;
class LobbySelector extends StatelessWidget {
const LobbySelector({super.key});
@ -12,16 +9,10 @@ class LobbySelector extends StatelessWidget {
return Scaffold(
body: TextButton(
onPressed: () {
var randomGameFuture = registerForRandomGame();
log(randomGameFuture.toString());
context.goNamed('prepareChessGame', extra: randomGameFuture);
context.goNamed('prepareChessGame');
},
child: const Text('Random lobby'),
),
);
}
Future<http.Response> registerForRandomGame() {
return http.get(Uri.parse('http://localhost:8080/api/random'));
}
}

View File

@ -1,24 +1,41 @@
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:mchess/models/models.dart';
import 'package:mchess/pages/chess_game.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
class PrepareChessGameWidget extends StatelessWidget {
final Future responseFutureForRegistering;
const PrepareChessGameWidget(
{required this.responseFutureForRegistering, super.key});
const PrepareChessGameWidget({super.key});
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: responseFutureForRegistering,
future: registerForRandomGame(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
log('future done');
return const ChessGame();
log('future done ${snapshot.data?.playerID}');
return ChessGame(
playerID: snapshot.data!.playerID,
);
}
return const CircularProgressIndicator();
},
);
}
Future<ResponseFromRegisteringGame> registerForRandomGame() async {
final response =
await http.get(Uri.parse('http://localhost:8080/api/random'));
if (response.statusCode == 200) {
log(response.body);
return ResponseFromRegisteringGame.fromJson(jsonDecode(response.body));
} else {
// If the server did not return a 200 OK response,
// then throw an exception.
throw Exception('Failed to load album');
}
}
}

View File

@ -22,8 +22,7 @@ class ChessAppRouter {
path: '/play',
name: 'prepareChessGame',
builder: (context, state) {
Future future = state.extra as Future;
return PrepareChessGameWidget(responseFutureForRegistering: future);
return const PrepareChessGameWidget();
})
],
);

View File

@ -301,6 +301,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.3.1"
uuid:
dependency: "direct main"
description:
name: uuid
sha256: "648e103079f7c64a36dc7d39369cabb358d377078a051d6ae2ad3aa539519313"
url: "https://pub.dev"
source: hosted
version: "3.0.7"
vector_math:
dependency: transitive
description:

View File

@ -43,6 +43,7 @@ dependencies:
web_socket_channel: ^2.2.0
go_router: ^6.0.0
http: ^1.0.0
uuid: ^3.0.7
dev_dependencies:
flutter_test:

View File

@ -8,11 +8,14 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mchess/pages/chess_game.dart';
import 'package:uuid/uuid.dart';
void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(const ChessGame());
await tester.pumpWidget(ChessGame(
playerID: UuidValue("test"),
));
// Verify that our counter starts at 0.
expect(find.text('0'), findsOneWidget);