From d487b15f561b96a7cc60bbf53dbd5440b2fb187e Mon Sep 17 00:00:00 2001 From: Jared Goodwin Date: Mon, 19 Jul 2021 14:29:42 -0700 Subject: [PATCH] Check result of Connect in MainWindowViewModel. --- Desktop.Win/ViewModels/MainWindowViewModel.cs | 69 +++++++++--------- .../ViewModels/MainWindowViewModel.cs | 71 ++++++++++--------- 2 files changed, 75 insertions(+), 65 deletions(-) diff --git a/Desktop.Win/ViewModels/MainWindowViewModel.cs b/Desktop.Win/ViewModels/MainWindowViewModel.cs index c140f813..fd6e70fd 100644 --- a/Desktop.Win/ViewModels/MainWindowViewModel.cs +++ b/Desktop.Win/ViewModels/MainWindowViewModel.cs @@ -206,48 +206,51 @@ namespace Remotely.Desktop.Win.ViewModels try { - await _casterSocket.Connect(_conductor.Host); + var result = await _casterSocket.Connect(_conductor.Host); - if (_casterSocket.Connection is null) + if (result) { + _casterSocket.Connection.Closed += (ex) => + { + App.Current?.Dispatcher?.Invoke(() => + { + Viewers.Clear(); + SessionID = "Disconnected"; + }); + return Task.CompletedTask; + }; + + _casterSocket.Connection.Reconnecting += (ex) => + { + App.Current?.Dispatcher?.Invoke(() => + { + Viewers.Clear(); + SessionID = "Reconnecting"; + }); + return Task.CompletedTask; + }; + + _casterSocket.Connection.Reconnected += async (arg) => + { + await GetSessionID(); + }; + + await DeviceInitService.GetInitParams(); + ApplyBranding(); + + await GetSessionID(); + return; } - - _casterSocket.Connection.Closed += async (ex) => - { - await App.Current?.Dispatcher?.InvokeAsync(() => - { - Viewers.Clear(); - SessionID = "Disconnected"; - }); - }; - - _casterSocket.Connection.Reconnecting += async (ex) => - { - await App.Current?.Dispatcher?.InvokeAsync(() => - { - Viewers.Clear(); - SessionID = "Reconnecting"; - }); - }; - - _casterSocket.Connection.Reconnected += async (arg) => - { - await GetSessionID(); - }; - - await DeviceInitService.GetInitParams(); - ApplyBranding(); - - await GetSessionID(); } catch (Exception ex) { Logger.Write(ex); - SessionID = "Failed"; - MessageBox.Show(Application.Current.MainWindow, "Failed to connect to server.", "Connection Failed", MessageBoxButton.OK, MessageBoxImage.Warning); - return; } + + // If we got here, something went wrong. + SessionID = "Failed"; + MessageBox.Show(Application.Current.MainWindow, "Failed to connect to server.", "Connection Failed", MessageBoxButton.OK, MessageBoxImage.Warning); } public void PromptForHostName() diff --git a/Desktop.XPlat/ViewModels/MainWindowViewModel.cs b/Desktop.XPlat/ViewModels/MainWindowViewModel.cs index 6d5d040c..aa0224f6 100644 --- a/Desktop.XPlat/ViewModels/MainWindowViewModel.cs +++ b/Desktop.XPlat/ViewModels/MainWindowViewModel.cs @@ -163,48 +163,55 @@ namespace Remotely.Desktop.XPlat.ViewModels _conductor.ProcessArgs(new string[] { "-mode", "Normal", "-host", Host }); - await _casterSocket.Connect(_conductor.Host); + var result = await _casterSocket.Connect(_conductor.Host); - if (_casterSocket.Connection is null) + if (result) { + if (_casterSocket.Connection is null) + { + return; + } + + _casterSocket.Connection.Closed += async (ex) => + { + await Dispatcher.UIThread.InvokeAsync(() => + { + SessionID = "Disconnected"; + }); + }; + + _casterSocket.Connection.Reconnecting += async (ex) => + { + await Dispatcher.UIThread.InvokeAsync(() => + { + SessionID = "Reconnecting"; + }); + }; + + _casterSocket.Connection.Reconnected += async (arg) => + { + await GetSessionID(); + }; + + await DeviceInitService.GetInitParams(); + + ApplyBranding(); + + await _casterSocket.SendDeviceInfo(_conductor.ServiceID, Environment.MachineName, _conductor.DeviceID); + await _casterSocket.GetSessionID(); + return; } - _casterSocket.Connection.Closed += async (ex) => - { - await Dispatcher.UIThread.InvokeAsync(() => - { - SessionID = "Disconnected"; - }); - }; - - _casterSocket.Connection.Reconnecting += async (ex) => - { - await Dispatcher.UIThread.InvokeAsync(() => - { - SessionID = "Reconnecting"; - }); - }; - - _casterSocket.Connection.Reconnected += async (arg) => - { - await GetSessionID(); - }; - - await DeviceInitService.GetInitParams(); - - ApplyBranding(); - - await _casterSocket.SendDeviceInfo(_conductor.ServiceID, Environment.MachineName, _conductor.DeviceID); - await _casterSocket.GetSessionID(); } catch (Exception ex) { Logger.Write(ex); - _sessionID = "Failed"; - await MessageBox.Show("Failed to connect to server.", "Connection Failed", MessageBoxType.OK); - return; } + + // If we got here, something went wrong. + _sessionID = "Failed"; + await MessageBox.Show("Failed to connect to server.", "Connection Failed", MessageBoxType.OK); } public async Task PromptForHostName()