/// <summary> /// Calls the StartServer method with Task.Run to not /// block the UI thread. /// </summary> private void ButtonStart_Click(object sender, EventArgs e) { this.serverUri = ServerUriBuilder.PrepareServerUri(this.serverUriTemplate, textBox1.Text); WriteToConsole("Starting server..."); ButtonStart.Enabled = false; try { Task.Run(() => server.StartServer(this.serverUri)); } catch (TargetInvocationException) { WriteToConsole("Server failed to start. A server is already running on " + this.serverUri); //Re-enable button to let user try to start server again this.Invoke((Action)(() => ButtonStart.Enabled = true)); return; } this.Invoke((Action)(() => ButtonStop.Enabled = true)); WriteToConsole("Server started at " + this.serverUri); }
/// <summary> /// Creates and connects the hub connection and hub proxy. This method /// is called asynchronously from SignInButton_Click. /// </summary> private async void ConnectAsync() { string ServerURI = String.Empty; try { ServerURI = ServerUriBuilder.GetServerUriForClient(this.textBox1.Text); } catch (Exception ex) { MessageBox.Show($"Podany adres jest nieprawidłowy. Wyjątek: {ex.Message}"); return; } Connection = new HubConnection(ServerURI); Connection.Closed += Connection_Closed; HubProxy = Connection.CreateHubProxy("MyHub"); //Handle incoming event from server: use Invoke to write to console from SignalR's thread HubProxy.On <string, string>("AddMessage", (name, message) => this.Invoke((Action)(() => RichTextBoxConsole.AppendText(String.Format("{0} {1}: {2}" + Environment.NewLine, name, DateTime.Now, message)) )) ); try { await Connection.Start(); } catch (HttpRequestException) { StatusText.Text = "Unable to connect to server: Start server before connecting clients."; //No connection: Don't enable Send button or show chat UI return; } //Activate UI SignInPanel.Visible = false; ChatPanel.Visible = true; ButtonSend.Enabled = true; TextBoxMessage.Focus(); RichTextBoxConsole.AppendText("Connected to server at " + ServerURI + Environment.NewLine); }
public void ShouldBuildCorrectServerUri() { var serverUri = ServerUriBuilder.PrepareServerUri("http://test:{0}", "2020").ToString(); Assert.AreEqual("http://test:2020", serverUri); }
public void ShouldReturnDefaultUriWhenPortIsntGiven() { var serverUri = ServerUriBuilder.PrepareServerUri("http://test:{0}", "").ToString(); Assert.AreEqual("http://test:8080", serverUri); }