static void Main(string[] args) { Connection = new HubConnection(ServerURI); HubProxy = Connection.CreateHubProxy("MyHub"); HubProxy.On <string, string>("SendMessage", (name, message) => Console.WriteLine(name + ":" + message)); Connection.Start().Wait(); Console.WriteLine("Press Enter to stop client"); Console.ReadLine(); }
/// <summary> /// Creates and connects the hub connection and hub proxy. This method /// is called asynchronously from SignInButton_Click. /// </summary> private async void ConnectAsync() { 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>("AddUserMessage", (name, message) => this.Dispatcher.Invoke(() => RichTextBoxConsole.AppendText(String.Format("{0}: {1}\r", name, message)) ) ); HubProxy.On <string>("AddMessage", (message) => this.Dispatcher.Invoke(() => RichTextBoxConsole.AppendText(String.Format("{0}\r", message)) ) ); try { await Connection.Start(); } catch (HttpRequestException) { StatusText.Content = "Unable to connect to server: Start server before connecting clients."; //No connection: Don't enable Send button or show chat UI return; } //Show chat UI; hide login UI SignInPanel.Visibility = Visibility.Collapsed; ChatPanel.Visibility = Visibility.Visible; ButtonSend.IsEnabled = true; ButtonSendAnoymous.IsEnabled = true; TextBoxMessage.Focus(); RichTextBoxConsole.AppendText("Connected to server at " + ServerURI + "\r"); }
private void BindMethodToProxy(HubProxy proxy) { proxy.On <string>("ReceiveMessage", (message) => ((IRobotClient)Client).ReceiveMessage(message)); }