mchess-client/lib/connection/ws_connection.dart

43 lines
979 B
Dart

import 'dart:developer';
import 'package:web_socket_channel/web_socket_channel.dart';
class ServerConnection {
late WebSocketChannel channel;
late bool wasConnected = false;
late int counter = 0;
static final ServerConnection _instance = ServerConnection._internal();
ServerConnection._internal() {
log("ServerConnection._interal constructor is called");
connect();
}
factory ServerConnection() {
return _instance;
}
factory ServerConnection.getInstance() {
return ServerConnection();
}
void send(String message) {
channel.sink.add('$counter $message');
counter++;
}
void connect() {
if (wasConnected) channel.sink.close();
channel = WebSocketChannel.connect(Uri.parse('ws://localhost:8080'));
wasConnected = true;
sendPassword();
}
void sendPassword() {
channel.sink.add(
'pw NC4EjHvRcsltY3ibWuYDH9OXbKgDXppfnHNCi1K4jktMspoeZjBnOPExxCpDms7zmxijoKCSaSlZ1fWclfWr7Q32DJnv2k87Z5uM');
}
}