Add class that handles server connections via web sockets.
This commit is contained in:
parent
05980a6b63
commit
76e141619b
27
lib/connection/ws_connection.dart
Normal file
27
lib/connection/ws_connection.dart
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
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() {
|
||||||
|
channel = WebSocketChannel.connect(
|
||||||
|
Uri.parse('ws://localhost:8080'),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
factory ServerConnection() {
|
||||||
|
return _instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
factory ServerConnection.getInstance() {
|
||||||
|
return ServerConnection();
|
||||||
|
}
|
||||||
|
|
||||||
|
void send(String message) {
|
||||||
|
channel.sink.add('$message: $counter');
|
||||||
|
counter++;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user