2022-11-13 13:25:47 +00:00
|
|
|
import 'package:web_socket_channel/web_socket_channel.dart';
|
|
|
|
|
|
|
|
class ServerConnection {
|
|
|
|
late WebSocketChannel channel;
|
2022-12-13 02:36:22 +00:00
|
|
|
late bool wasConnected = false;
|
2022-11-13 13:25:47 +00:00
|
|
|
late int counter = 0;
|
|
|
|
|
|
|
|
static final ServerConnection _instance = ServerConnection._internal();
|
|
|
|
|
|
|
|
ServerConnection._internal() {
|
2022-11-19 12:24:38 +00:00
|
|
|
print("ServerConnection._interal constructor is called");
|
2022-12-13 02:36:22 +00:00
|
|
|
connect();
|
2022-11-13 13:25:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
factory ServerConnection() {
|
|
|
|
return _instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
factory ServerConnection.getInstance() {
|
|
|
|
return ServerConnection();
|
|
|
|
}
|
|
|
|
|
|
|
|
void send(String message) {
|
2022-11-19 12:24:38 +00:00
|
|
|
channel.sink.add('$counter $message');
|
2022-11-13 13:25:47 +00:00
|
|
|
counter++;
|
|
|
|
}
|
2022-11-19 12:24:38 +00:00
|
|
|
|
2022-12-13 02:36:22 +00:00
|
|
|
void connect() {
|
|
|
|
if (wasConnected) channel.sink.close();
|
2022-11-19 12:24:38 +00:00
|
|
|
channel = WebSocketChannel.connect(Uri.parse('ws://localhost:8080'));
|
2022-12-13 02:36:22 +00:00
|
|
|
wasConnected = true;
|
|
|
|
|
|
|
|
sendPassword();
|
|
|
|
}
|
|
|
|
|
|
|
|
void sendPassword() {
|
|
|
|
channel.sink.add(
|
|
|
|
'pw NC4EjHvRcsltY3ibWuYDH9OXbKgDXppfnHNCi1K4jktMspoeZjBnOPExxCpDms7zmxijoKCSaSlZ1fWclfWr7Q32DJnv2k87Z5uM');
|
2022-11-19 12:24:38 +00:00
|
|
|
}
|
2022-11-13 13:25:47 +00:00
|
|
|
}
|