Change connection handling (Use playerID from registering to connect via websocket.
This commit is contained in:
parent
299b77d249
commit
b0d6f4002c
@ -31,7 +31,7 @@ class ServerConnection {
|
|||||||
counter++;
|
counter++;
|
||||||
}
|
}
|
||||||
|
|
||||||
void connect() {
|
void connect(String playerID) {
|
||||||
if (wasConnected) channel.sink.close();
|
if (wasConnected) channel.sink.close();
|
||||||
|
|
||||||
if (kDebugMode) {
|
if (kDebugMode) {
|
||||||
@ -41,6 +41,7 @@ class ServerConnection {
|
|||||||
channel =
|
channel =
|
||||||
WebSocketChannel.connect(Uri.parse('wss://chess.sw-gross.de:8080'));
|
WebSocketChannel.connect(Uri.parse('wss://chess.sw-gross.de:8080'));
|
||||||
}
|
}
|
||||||
|
send(playerID);
|
||||||
|
|
||||||
log(channel.closeCode.toString());
|
log(channel.closeCode.toString());
|
||||||
|
|
||||||
|
@ -15,8 +15,8 @@ class ConnectionCubit extends Cubit<ConnectionCubitState> {
|
|||||||
return _instance;
|
return _instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
void connect() {
|
void connect(String playerID) {
|
||||||
ServerConnection.getInstance().connect();
|
ServerConnection.getInstance().connect(playerID);
|
||||||
emit(ConnectionCubitState(true));
|
emit(ConnectionCubitState(true));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
17
lib/models/models.dart
Normal file
17
lib/models/models.dart
Normal 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,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_bloc/flutter_bloc.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_bloc/chess_bloc.dart';
|
||||||
|
|
||||||
import 'package:mchess/chess/chess_board.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/connection/ws_connection.dart';
|
||||||
import 'package:mchess/utils/widgets/server_log_widget.dart';
|
import 'package:mchess/utils/widgets/server_log_widget.dart';
|
||||||
|
import 'package:uuid/uuid.dart';
|
||||||
|
|
||||||
class ChessGame extends StatefulWidget {
|
class ChessGame extends StatefulWidget {
|
||||||
const ChessGame({super.key});
|
final UuidValue playerID;
|
||||||
|
const ChessGame({required this.playerID, super.key});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<ChessGame> createState() => _ChessGameState();
|
State<ChessGame> createState() => _ChessGameState();
|
||||||
@ -21,7 +24,7 @@ class _ChessGameState extends State<ChessGame> {
|
|||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
ConnectionCubit.getInstance().connect();
|
ConnectionCubit.getInstance().connect(widget.playerID.uuid);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@ -59,9 +62,9 @@ class _ChessGameState extends State<ChessGame> {
|
|||||||
),
|
),
|
||||||
floatingActionButton: FloatingActionButton(
|
floatingActionButton: FloatingActionButton(
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
ConnectionCubit.getInstance().connect();
|
context.goNamed('lobbySelector');
|
||||||
},
|
},
|
||||||
child: const Icon(Icons.network_wifi),
|
child: const Icon(Icons.arrow_back),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,5 @@
|
|||||||
import 'dart:developer';
|
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:go_router/go_router.dart';
|
import 'package:go_router/go_router.dart';
|
||||||
import 'package:http/http.dart' as http;
|
|
||||||
|
|
||||||
class LobbySelector extends StatelessWidget {
|
class LobbySelector extends StatelessWidget {
|
||||||
const LobbySelector({super.key});
|
const LobbySelector({super.key});
|
||||||
@ -12,16 +9,10 @@ class LobbySelector extends StatelessWidget {
|
|||||||
return Scaffold(
|
return Scaffold(
|
||||||
body: TextButton(
|
body: TextButton(
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
var randomGameFuture = registerForRandomGame();
|
context.goNamed('prepareChessGame');
|
||||||
log(randomGameFuture.toString());
|
|
||||||
context.goNamed('prepareChessGame', extra: randomGameFuture);
|
|
||||||
},
|
},
|
||||||
child: const Text('Random lobby'),
|
child: const Text('Random lobby'),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<http.Response> registerForRandomGame() {
|
|
||||||
return http.get(Uri.parse('http://localhost:8080/api/random'));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,24 +1,41 @@
|
|||||||
import 'dart:developer';
|
import 'dart:developer';
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:mchess/models/models.dart';
|
||||||
import 'package:mchess/pages/chess_game.dart';
|
import 'package:mchess/pages/chess_game.dart';
|
||||||
|
import 'package:http/http.dart' as http;
|
||||||
|
import 'dart:convert';
|
||||||
|
|
||||||
class PrepareChessGameWidget extends StatelessWidget {
|
class PrepareChessGameWidget extends StatelessWidget {
|
||||||
final Future responseFutureForRegistering;
|
const PrepareChessGameWidget({super.key});
|
||||||
const PrepareChessGameWidget(
|
|
||||||
{required this.responseFutureForRegistering, super.key});
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return FutureBuilder(
|
return FutureBuilder(
|
||||||
future: responseFutureForRegistering,
|
future: registerForRandomGame(),
|
||||||
builder: (context, snapshot) {
|
builder: (context, snapshot) {
|
||||||
if (snapshot.connectionState == ConnectionState.done) {
|
if (snapshot.connectionState == ConnectionState.done) {
|
||||||
log('future done');
|
log('future done ${snapshot.data?.playerID}');
|
||||||
return const ChessGame();
|
return ChessGame(
|
||||||
|
playerID: snapshot.data!.playerID,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
return const CircularProgressIndicator();
|
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');
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,8 +22,7 @@ class ChessAppRouter {
|
|||||||
path: '/play',
|
path: '/play',
|
||||||
name: 'prepareChessGame',
|
name: 'prepareChessGame',
|
||||||
builder: (context, state) {
|
builder: (context, state) {
|
||||||
Future future = state.extra as Future;
|
return const PrepareChessGameWidget();
|
||||||
return PrepareChessGameWidget(responseFutureForRegistering: future);
|
|
||||||
})
|
})
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
@ -301,6 +301,14 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.3.1"
|
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:
|
vector_math:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
@ -43,6 +43,7 @@ dependencies:
|
|||||||
web_socket_channel: ^2.2.0
|
web_socket_channel: ^2.2.0
|
||||||
go_router: ^6.0.0
|
go_router: ^6.0.0
|
||||||
http: ^1.0.0
|
http: ^1.0.0
|
||||||
|
uuid: ^3.0.7
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
@ -8,11 +8,14 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_test/flutter_test.dart';
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
import 'package:mchess/pages/chess_game.dart';
|
import 'package:mchess/pages/chess_game.dart';
|
||||||
|
import 'package:uuid/uuid.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
|
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
|
||||||
// Build our app and trigger a frame.
|
// 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.
|
// Verify that our counter starts at 0.
|
||||||
expect(find.text('0'), findsOneWidget);
|
expect(find.text('0'), findsOneWidget);
|
||||||
|
Loading…
Reference in New Issue
Block a user