From 76e141619b06bad07fe9928b93e3a09ac8443b5c Mon Sep 17 00:00:00 2001 From: Marco Date: Sun, 13 Nov 2022 14:25:47 +0100 Subject: [PATCH] Add class that handles server connections via web sockets. --- lib/connection/ws_connection.dart | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 lib/connection/ws_connection.dart diff --git a/lib/connection/ws_connection.dart b/lib/connection/ws_connection.dart new file mode 100644 index 0000000..2b3e3c0 --- /dev/null +++ b/lib/connection/ws_connection.dart @@ -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++; + } +}