/// <summary> /// Check if the client is connected to a server /// </summary> /// <param name="delayBetweenCheck">The delay between every check in milliseconds</param> /// <param name="waitTime">The time needed for every ping to the server in milliseconds</param> public void StartConnectionListener(int delayBetweenCheck = 1000, int waitTime = 100) { // Make the command Command connectionChecker = new Command(async() => { while (true) { if (RCClientHelpers.PingServer(RCClientHelpers.LatestIPAddress, RCClientHelpers.ServerPort, waitTime)) { if (Client.SingletonRCClient.SingleRCClient.IsConnected) { this.CurrentPage = ApplicationPage.ControllerSelect; } } else { this.CurrentPage = ApplicationPage.Login; Client.SingletonRCClient.SingleRCClient.TcpClient.Disconnect(); } await Task.Delay(delayBetweenCheck); } }); // Start the listener connectionChecker.Execute(null); }
/// <summary> /// Make the commands needed for this view model /// </summary> private void MakeCommands() { this.DetectIPButtonClicked = new Command(async() => { await Task.Run(() => { this.IsBusy = true; this.IPBoxText = Client.SingletonRCClient.SingleRCClient.GetActiveServerIP(); this.IsBusy = false; }); }); this.ConnectButtonClicked = new Command(async() => { await Task.Run(() => { if (!String.IsNullOrWhiteSpace(this.IPBoxText)) { this.IsBusy = true; try { if (RCClientHelpers.PingServer(this.IPBoxText, RCClientHelpers.ServerPort, 200)) { Client.SingletonRCClient.SingleRCClient.Connect(this.IPBoxText, RCClientHelpers.ServerPort); RCClientHelpers.LatestIPAddress = this.IPBoxText; } } catch (SocketException) { this.InfoMessage = "Connection failed!"; this.IsBusy = false; } finally { this.IsBusy = false; } } }); }); this.ClearInfoMessageClicked = new Command(async() => { await Task.Run(() => { this.InfoMessage = ""; }); }); }