@@ -8,15 +8,17 @@ import 'package:web_socket_channel/web_socket_channel.dart';
88/// Connection state for the SDK.
99enum VideSdkConnectionState { disconnected, connecting, connected, error }
1010
11- const _kServerUrlKey = 'vide_sdk_server_url' ;
11+ const _kHostKey = 'vide_sdk_host' ;
12+ const _kPortKey = 'vide_sdk_port' ;
1213const _kWorkingDirKey = 'vide_sdk_working_directory' ;
1314
1415/// Central state for the Vide in-app SDK.
1516///
1617/// Manages the lifecycle of the [VideClient] and [RemoteVideSession] ,
1718/// exposes connection state, and provides the active session to the UI.
1819class VideSdkState extends ChangeNotifier {
19- String ? _serverUrl;
20+ String ? _host;
21+ int ? _port;
2022 String ? _workingDirectory;
2123
2224 VideClient ? _client;
@@ -25,15 +27,18 @@ class VideSdkState extends ChangeNotifier {
2527 String ? _errorMessage;
2628 StreamSubscription <VideEvent >? _eventSubscription;
2729
28- VideSdkState ({String ? serverUrl, String ? workingDirectory})
29- : _serverUrl = serverUrl,
30+ VideSdkState ({String ? host, int ? port, String ? workingDirectory})
31+ : _host = host,
32+ _port = port,
3033 _workingDirectory = workingDirectory;
3134
32- String ? get serverUrl => _serverUrl;
35+ String ? get host => _host;
36+ int ? get port => _port;
3337 String ? get workingDirectory => _workingDirectory;
3438 bool get isConfigured =>
35- _serverUrl != null &&
36- _serverUrl! .isNotEmpty &&
39+ _host != null &&
40+ _host! .isNotEmpty &&
41+ _port != null &&
3742 _workingDirectory != null &&
3843 _workingDirectory! .isNotEmpty;
3944
@@ -47,34 +52,38 @@ class VideSdkState extends ChangeNotifier {
4752 /// Load persisted configuration from shared preferences.
4853 Future <void > loadConfig () async {
4954 final prefs = await SharedPreferences .getInstance ();
50- _serverUrl ?? = prefs.getString (_kServerUrlKey);
55+ _host ?? = prefs.getString (_kHostKey);
56+ _port ?? = prefs.getInt (_kPortKey);
5157 _workingDirectory ?? = prefs.getString (_kWorkingDirKey);
5258 notifyListeners ();
5359 }
5460
5561 /// Update and persist configuration.
5662 Future <void > updateConfig ({
57- required String serverUrl,
63+ required String host,
64+ required int port,
5865 required String workingDirectory,
5966 }) async {
60- _serverUrl = serverUrl;
67+ _host = host;
68+ _port = port;
6169 _workingDirectory = workingDirectory;
6270
6371 final prefs = await SharedPreferences .getInstance ();
64- await prefs.setString (_kServerUrlKey, serverUrl);
72+ await prefs.setString (_kHostKey, host);
73+ await prefs.setInt (_kPortKey, port);
6574 await prefs.setString (_kWorkingDirKey, workingDirectory);
6675
6776 // Disconnect any existing session since config changed
6877 await disconnect ();
6978 notifyListeners ();
7079 }
7180
72- /// Parse host and port from the server URL .
73- ( String host, int port) get _parsedUrl {
74- var url = _serverUrl ! ;
75- if ( ! url. startsWith ( 'http' )) url = 'http://$ url ' ;
76- final uri = Uri . parse (url );
77- return (uri.host, uri.port );
81+ /// Test connection to a server by checking its health endpoint .
82+ ///
83+ /// Returns true if the server is reachable and healthy.
84+ Future < bool > testConnection ({ required String host, required int port}) async {
85+ final client = VideClient (host : host, port : port );
86+ return client. checkHealth ( );
7887 }
7988
8089 /// Connect to the server and create a new session with the given message.
@@ -86,8 +95,7 @@ class VideSdkState extends ChangeNotifier {
8695 _errorMessage = null ;
8796 notifyListeners ();
8897
89- final (host, port) = _parsedUrl;
90- _client = VideClient (host: host, port: port);
98+ _client = VideClient (host: _host! , port: _port! );
9199
92100 final pending = createPendingRemoteVideSession (
93101 initialMessage: initialMessage,
@@ -123,8 +131,7 @@ class VideSdkState extends ChangeNotifier {
123131 _errorMessage = null ;
124132 notifyListeners ();
125133
126- final (host, port) = _parsedUrl;
127- _client = VideClient (host: host, port: port);
134+ _client = VideClient (host: _host! , port: _port! );
128135
129136 try {
130137 _session = await _client! .connectToSession (sessionId);
0 commit comments