Esempio n. 1
0
        /// <summary>
        /// Creates and connects the connection. Only uses one controller (chat). This method
        /// is called from SignInButton_Click.
        /// </summary>
        private async void Connect()
        {
            Connection = new XSocketClient(ServerURI, Origin, "chat");
            Connection.OnDisconnected += Connection_Disconnected;
            chatController             = Connection.Controller("chat");

            //Handle incoming event from server: use Invoke to write to console from XSocket's thread
            chatController.On <string>("addMessage", message => this.Invoke((Action)(() =>
                                                                                     RichTextBoxConsole.AppendText(String.Format("{0}" + Environment.NewLine, message))
                                                                                     )));

            try
            {
                Connection.Open();
                comboBoxLocation.SelectedIndex = 0;
                //Set username
                chatController.SetProperty("username", UserName);
            }
            catch
            {
                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);
        }
Esempio n. 2
0
        /// <summary>
        /// Creates and connects the connection. Only uses one controller (chat). This method
        /// is called from SignInButton_Click.
        /// </summary>
        private async void Connect()
        {
            Connection = new XSocketClient(ServerURI,Origin,"chat");
            Connection.OnDisconnected += Connection_Disconnected;
            chatController = Connection.Controller("chat");            
            
            //Handle incoming event from server: use Invoke to write to console from XSocket's thread
            chatController.On<string>("addMessage", message => this.Invoke((Action) (() =>
                RichTextBoxConsole.AppendText(String.Format("{0}" + Environment.NewLine, message))
            )));
            
            try
            {
                Connection.Open();
                comboBoxLocation.SelectedIndex = 0;
                //Set username
                chatController.SetProperty("username", UserName);
            }
            catch
            {
                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);
        }
Esempio n. 3
0
        /// <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 XSocketClient(ServerURI, Origin, "chat");
            Connection.OnDisconnected += Connection_Disconnected;
            chatController             = Connection.Controller("chat");

            //Handle incoming event from server: use Invoke to write to console from XSocket's thread
            chatController.On <string>("AddMessage", (message) =>
                                       this.Dispatcher.Invoke(() =>
                                                              RichTextBoxConsole.AppendText(String.Format("{0}\r", message))
                                                              )
                                       );

            try
            {
                await Connection.Open();

                //Set username
                await chatController.SetProperty("username", UserName);
            }
            catch
            {
                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;
            TextBoxMessage.Focus();
            RichTextBoxConsole.AppendText("Connected to server at " + ServerURI + "\r");
        }
Esempio n. 4
0
        /// <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 XSocketClient(ServerURI, Origin, "chat");
            Connection.OnDisconnected += Connection_Disconnected;
            chatController = Connection.Controller("chat");

            //Handle incoming event from server: use Invoke to write to console from XSocket's thread
            chatController.On<string>("AddMessage", (message) =>
                this.Dispatcher.Invoke(() =>
                    RichTextBoxConsole.AppendText(String.Format("{0}\r", message))
                )
            );

            try
            {
                Connection.Open();

                //Set username
                chatController.SetProperty("username", UserName);
            }
            catch
            {
                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;
            TextBoxMessage.Focus();
            RichTextBoxConsole.AppendText("Connected to server at " + ServerURI + "\r");
        }