Add reconnect mechanism.

This commit is contained in:
Marco 2022-11-19 13:24:38 +01:00
parent 1dab094d19
commit 4768d22168
2 changed files with 14 additions and 1 deletions

View File

@ -56,7 +56,15 @@ class ChessApp extends StatelessWidget {
),
),
),
floatingActionButton: const FloatingActionButton(
onPressed: reconnect,
child: Icon(Icons.connect_without_contact),
),
),
);
}
}
void reconnect() {
ServerConnection.getInstance().reconnect();
}

View File

@ -7,6 +7,7 @@ class ServerConnection {
static final ServerConnection _instance = ServerConnection._internal();
ServerConnection._internal() {
print("ServerConnection._interal constructor is called");
channel = WebSocketChannel.connect(
Uri.parse('ws://localhost:8080'),
);
@ -21,7 +22,11 @@ class ServerConnection {
}
void send(String message) {
channel.sink.add('$message: $counter');
channel.sink.add('$counter $message');
counter++;
}
void reconnect() {
channel = WebSocketChannel.connect(Uri.parse('ws://localhost:8080'));
}
}