Add server password for connection, and change reconnect handling.
This commit is contained in:
parent
3889245c8a
commit
8a0139640c
@ -51,6 +51,7 @@ class ChessApp extends StatelessWidget {
|
|||||||
return StreamBuilder(
|
return StreamBuilder(
|
||||||
stream: ServerConnection.getInstance().channel.stream,
|
stream: ServerConnection.getInstance().channel.stream,
|
||||||
builder: (context, snapshot) {
|
builder: (context, snapshot) {
|
||||||
|
print(snapshot.data.toString());
|
||||||
return Text(
|
return Text(
|
||||||
style: const TextStyle(color: Colors.white),
|
style: const TextStyle(color: Colors.white),
|
||||||
snapshot.data.toString());
|
snapshot.data.toString());
|
||||||
@ -65,7 +66,6 @@ class ChessApp extends StatelessWidget {
|
|||||||
),
|
),
|
||||||
floatingActionButton: FloatingActionButton(
|
floatingActionButton: FloatingActionButton(
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
ServerConnection.getInstance().reconnect();
|
|
||||||
ConnectionCubit.getInstance().reconnect();
|
ConnectionCubit.getInstance().reconnect();
|
||||||
},
|
},
|
||||||
child: const Icon(Icons.network_wifi),
|
child: const Icon(Icons.network_wifi),
|
||||||
|
@ -2,15 +2,14 @@ import 'package:web_socket_channel/web_socket_channel.dart';
|
|||||||
|
|
||||||
class ServerConnection {
|
class ServerConnection {
|
||||||
late WebSocketChannel channel;
|
late WebSocketChannel channel;
|
||||||
|
late bool wasConnected = false;
|
||||||
late int counter = 0;
|
late int counter = 0;
|
||||||
|
|
||||||
static final ServerConnection _instance = ServerConnection._internal();
|
static final ServerConnection _instance = ServerConnection._internal();
|
||||||
|
|
||||||
ServerConnection._internal() {
|
ServerConnection._internal() {
|
||||||
print("ServerConnection._interal constructor is called");
|
print("ServerConnection._interal constructor is called");
|
||||||
channel = WebSocketChannel.connect(
|
connect();
|
||||||
Uri.parse('ws://localhost:8080'),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
factory ServerConnection() {
|
factory ServerConnection() {
|
||||||
@ -26,7 +25,16 @@ class ServerConnection {
|
|||||||
counter++;
|
counter++;
|
||||||
}
|
}
|
||||||
|
|
||||||
void reconnect() {
|
void connect() {
|
||||||
|
if (wasConnected) channel.sink.close();
|
||||||
channel = WebSocketChannel.connect(Uri.parse('ws://localhost:8080'));
|
channel = WebSocketChannel.connect(Uri.parse('ws://localhost:8080'));
|
||||||
|
wasConnected = true;
|
||||||
|
|
||||||
|
sendPassword();
|
||||||
|
}
|
||||||
|
|
||||||
|
void sendPassword() {
|
||||||
|
channel.sink.add(
|
||||||
|
'pw NC4EjHvRcsltY3ibWuYDH9OXbKgDXppfnHNCi1K4jktMspoeZjBnOPExxCpDms7zmxijoKCSaSlZ1fWclfWr7Q32DJnv2k87Z5uM');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
|
|
||||||
|
import '../connection/ws_connection.dart';
|
||||||
|
|
||||||
class ConnectionCubit extends Cubit<ConnectionCubitState> {
|
class ConnectionCubit extends Cubit<ConnectionCubitState> {
|
||||||
static final ConnectionCubit _instance = ConnectionCubit._internal();
|
static final ConnectionCubit _instance = ConnectionCubit._internal();
|
||||||
|
|
||||||
@ -14,6 +16,7 @@ class ConnectionCubit extends Cubit<ConnectionCubitState> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void reconnect() {
|
void reconnect() {
|
||||||
|
ServerConnection.getInstance().connect();
|
||||||
emit(ConnectionCubitState(true));
|
emit(ConnectionCubitState(true));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user