Example #1
0
        /// <summary>
        /// Logs a _client to the server or logs them out when pressing the login button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (!_isConnected)
            {
                if (txtBoxNickName.Text != "" && txtBoxServerIP.Text != "")
                {
                    bool _nickNameCanLogin = _communicationManager.IsUserRegistered(txtBoxNickName.Text);

                    if (!_nickNameCanLogin)
                    {
                        MessageBox.Show(@"You are not registered to Chat. Please register first.");//Popup a message box if the user is not registered.
                    }
                    else //If the user is registered, continues the logic
                    {
                        _client          = new TcpClient(); //Start a new TCP _client to connect to the server
                        txtChatText.Text = null;

                        if (_communicationManager.InitializeConnection(txtBoxServerIP.Text, _clientIPaddr,
                                                                       txtBoxNickName.Text, _client))
                        {
                            _username     = txtBoxNickName.Text; //Update the gui textbox with _client nickname
                            _clientIPaddr = txtBoxServerIP.Text; //Update the gui with the server IP (local address 127.0.0.1)
                            UpdateOnLogin(true);                 //Update the gui button states
                            _clientStatus = "connecting";
                        }
                        else
                        {
                            while (txtBoxNickName.Text == "")
                            {
                                MessageBox.Show(@"Please enter a valid nickname");
                            }
                            txtBoxNickName.Text = _username;
                        }
                    }
                }
            }
            else // We are connected, thus disconnect
            {
                try
                {
                    _clientStatus = "disconnecting";
                    if (!_communicationManager.CloseConnection(_username, _client, _clientStatus, _isConnected))
                    {
                        UpdateOnLogin(false);

                        if (!_isConnected)
                        {
                            MessageBox.Show(@"Disconnected");
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
Example #2
0
        /// <summary>
        /// Logs a _client to the server or logs them out when pressing the login button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (!_isConnected)
            {
                if (txtBoxNickName.Text != "" && txtBoxServerIP.Text != "")
                {
                    _client          = new TcpClient(); //Start a new TCP _client to connect to the server
                    txtChatText.Text = null;

                    if (_communicationManager.InitializeConnection(txtBoxServerIP.Text, _clientIPaddr,
                                                                   txtBoxNickName.Text, _client))
                    {
                        _username     = txtBoxNickName.Text; //Update the gui textbox with _client nickname
                        _clientIPaddr = txtBoxServerIP.Text; //Update the gui with the server IP (local address 127.0.0.1)
                        UpdateOnLogin(true);                 //Update the gui button states
                        _clientStatus = "connecting";
                    }
                    else
                    {
                        while (txtBoxNickName.Text == "")
                        {
                            MessageBox.Show(@"Please enter your nickname");
                        }
                        txtBoxNickName.Text = _username;
                    }
                }
            }
            else // We are connected, thus disconnect
            {
                //TODO add another if to check close was ok
                try
                {
                    _clientStatus = "disconnecting";
                    if (!_communicationManager.CloseConnection(_username, _client, _clientStatus, _isConnected))
                    {
                        UpdateOnLogin(false);
                        //_isConnected = _client.Connected;

                        if (!_isConnected)
                        {
                            MessageBox.Show(@"Disconnected");
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }