From 01cd8a514349a1c4012d8691af19ce1f2c886bba Mon Sep 17 00:00:00 2001 From: Michael Jansen Date: Wed, 26 Jul 2023 18:30:42 -0400 Subject: [PATCH] add method to change the host address --- lib/src/nt4_client.dart | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/src/nt4_client.dart b/lib/src/nt4_client.dart index d0eb87d..2e0f88f 100644 --- a/lib/src/nt4_client.dart +++ b/lib/src/nt4_client.dart @@ -7,7 +7,7 @@ import 'package:msgpack_dart/msgpack_dart.dart'; import 'package:web_socket_channel/web_socket_channel.dart'; class NT4Client { - final String serverBaseAddress; + String serverBaseAddress; final void Function()? onConnect; final void Function()? onDisconnect; @@ -21,7 +21,6 @@ class NT4Client { // ignore: unused_field late Timer _timeSyncBgEvent; int _clientId = 0; - String _serverAddr = ''; bool _serverConnectionActive = false; int _serverTimeOffsetUS = 0; @@ -47,6 +46,15 @@ class NT4Client { _wsConnect(); } + /// Change the host address of the NT4 server + /// + /// This will attempt to close the current connection, then connect with + /// the new address + void setServerBaseAddress(String serverBaseAddress) { + this.serverBaseAddress = serverBaseAddress; + _wsOnClose(); + } + /// Create a stream that represents the status of the connection /// to the NT4 server Stream connectionStatusStream() async* { @@ -251,12 +259,9 @@ class NT4Client { void _wsConnect() async { _clientId = Random().nextInt(99999999); - int port = 5810; - String prefix = 'ws://'; - - _serverAddr = '$prefix$serverBaseAddress:$port/nt/DartClient_$_clientId'; + String serverAddr = 'ws://$serverBaseAddress:5810/nt/DartClient_$_clientId'; - _ws = WebSocketChannel.connect(Uri.parse(_serverAddr), + _ws = WebSocketChannel.connect(Uri.parse(serverAddr), protocols: ['networktables.first.wpi.edu']); try {