Skip to content

Commit d487b15

Browse files
committed
Check result of Connect in MainWindowViewModel.
1 parent f570b8a commit d487b15

File tree

2 files changed

+65
-55
lines changed

2 files changed

+65
-55
lines changed

Desktop.Win/ViewModels/MainWindowViewModel.cs

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -206,48 +206,51 @@ public async Task Init()
206206

207207
try
208208
{
209-
await _casterSocket.Connect(_conductor.Host);
209+
var result = await _casterSocket.Connect(_conductor.Host);
210210

211-
if (_casterSocket.Connection is null)
211+
if (result)
212212
{
213-
return;
214-
}
213+
_casterSocket.Connection.Closed += (ex) =>
214+
{
215+
App.Current?.Dispatcher?.Invoke(() =>
216+
{
217+
Viewers.Clear();
218+
SessionID = "Disconnected";
219+
});
220+
return Task.CompletedTask;
221+
};
215222

216-
_casterSocket.Connection.Closed += async (ex) =>
217-
{
218-
await App.Current?.Dispatcher?.InvokeAsync(() =>
223+
_casterSocket.Connection.Reconnecting += (ex) =>
219224
{
220-
Viewers.Clear();
221-
SessionID = "Disconnected";
222-
});
223-
};
225+
App.Current?.Dispatcher?.Invoke(() =>
226+
{
227+
Viewers.Clear();
228+
SessionID = "Reconnecting";
229+
});
230+
return Task.CompletedTask;
231+
};
224232

225-
_casterSocket.Connection.Reconnecting += async (ex) =>
226-
{
227-
await App.Current?.Dispatcher?.InvokeAsync(() =>
233+
_casterSocket.Connection.Reconnected += async (arg) =>
228234
{
229-
Viewers.Clear();
230-
SessionID = "Reconnecting";
231-
});
232-
};
235+
await GetSessionID();
236+
};
233237

234-
_casterSocket.Connection.Reconnected += async (arg) =>
235-
{
236-
await GetSessionID();
237-
};
238+
await DeviceInitService.GetInitParams();
239+
ApplyBranding();
238240

239-
await DeviceInitService.GetInitParams();
240-
ApplyBranding();
241+
await GetSessionID();
241242

242-
await GetSessionID();
243+
return;
244+
}
243245
}
244246
catch (Exception ex)
245247
{
246248
Logger.Write(ex);
247-
SessionID = "Failed";
248-
MessageBox.Show(Application.Current.MainWindow, "Failed to connect to server.", "Connection Failed", MessageBoxButton.OK, MessageBoxImage.Warning);
249-
return;
250249
}
250+
251+
// If we got here, something went wrong.
252+
SessionID = "Failed";
253+
MessageBox.Show(Application.Current.MainWindow, "Failed to connect to server.", "Connection Failed", MessageBoxButton.OK, MessageBoxImage.Warning);
251254
}
252255

253256
public void PromptForHostName()

Desktop.XPlat/ViewModels/MainWindowViewModel.cs

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -163,48 +163,55 @@ public async Task Init()
163163

164164
_conductor.ProcessArgs(new string[] { "-mode", "Normal", "-host", Host });
165165

166-
await _casterSocket.Connect(_conductor.Host);
166+
var result = await _casterSocket.Connect(_conductor.Host);
167167

168-
if (_casterSocket.Connection is null)
168+
if (result)
169169
{
170-
return;
171-
}
170+
if (_casterSocket.Connection is null)
171+
{
172+
return;
173+
}
172174

173-
_casterSocket.Connection.Closed += async (ex) =>
174-
{
175-
await Dispatcher.UIThread.InvokeAsync(() =>
175+
_casterSocket.Connection.Closed += async (ex) =>
176176
{
177-
SessionID = "Disconnected";
178-
});
179-
};
177+
await Dispatcher.UIThread.InvokeAsync(() =>
178+
{
179+
SessionID = "Disconnected";
180+
});
181+
};
180182

181-
_casterSocket.Connection.Reconnecting += async (ex) =>
182-
{
183-
await Dispatcher.UIThread.InvokeAsync(() =>
183+
_casterSocket.Connection.Reconnecting += async (ex) =>
184184
{
185-
SessionID = "Reconnecting";
186-
});
187-
};
185+
await Dispatcher.UIThread.InvokeAsync(() =>
186+
{
187+
SessionID = "Reconnecting";
188+
});
189+
};
188190

189-
_casterSocket.Connection.Reconnected += async (arg) =>
190-
{
191-
await GetSessionID();
192-
};
191+
_casterSocket.Connection.Reconnected += async (arg) =>
192+
{
193+
await GetSessionID();
194+
};
193195

194-
await DeviceInitService.GetInitParams();
196+
await DeviceInitService.GetInitParams();
195197

196-
ApplyBranding();
198+
ApplyBranding();
199+
200+
await _casterSocket.SendDeviceInfo(_conductor.ServiceID, Environment.MachineName, _conductor.DeviceID);
201+
await _casterSocket.GetSessionID();
202+
203+
return;
204+
}
197205

198-
await _casterSocket.SendDeviceInfo(_conductor.ServiceID, Environment.MachineName, _conductor.DeviceID);
199-
await _casterSocket.GetSessionID();
200206
}
201207
catch (Exception ex)
202208
{
203209
Logger.Write(ex);
204-
_sessionID = "Failed";
205-
await MessageBox.Show("Failed to connect to server.", "Connection Failed", MessageBoxType.OK);
206-
return;
207210
}
211+
212+
// If we got here, something went wrong.
213+
_sessionID = "Failed";
214+
await MessageBox.Show("Failed to connect to server.", "Connection Failed", MessageBoxType.OK);
208215
}
209216

210217
public async Task PromptForHostName()

0 commit comments

Comments
 (0)