33 lines
738 B
Dart
33 lines
738 B
Dart
import 'package:web_socket_channel/web_socket_channel.dart';
|
|
|
|
class ServerConnection {
|
|
late WebSocketChannel channel;
|
|
late int counter = 0;
|
|
|
|
static final ServerConnection _instance = ServerConnection._internal();
|
|
|
|
ServerConnection._internal() {
|
|
print("ServerConnection._interal constructor is called");
|
|
channel = WebSocketChannel.connect(
|
|
Uri.parse('ws://localhost:8080'),
|
|
);
|
|
}
|
|
|
|
factory ServerConnection() {
|
|
return _instance;
|
|
}
|
|
|
|
factory ServerConnection.getInstance() {
|
|
return ServerConnection();
|
|
}
|
|
|
|
void send(String message) {
|
|
channel.sink.add('$counter $message');
|
|
counter++;
|
|
}
|
|
|
|
void reconnect() {
|
|
channel = WebSocketChannel.connect(Uri.parse('ws://localhost:8080'));
|
|
}
|
|
}
|