Beispiel #1
0
 private void AppendText(string message)
 {
     if (chatTextBox.InvokeRequired)
     {
         var d = new FillChatDelegate(AppendText);
         chatTextBox.Invoke(d, new object[] { message });
     }
     else
     {
         chatTextBox.AppendText($"{message}{Environment.NewLine}");
     }
 }
Beispiel #2
0
        private void ConnectButton_Click(object sender, EventArgs e)
        {
            int.TryParse(portTextBox.Text, out var port);
            IPAddress.TryParse(ipAddressTextBox.Text, out var ip);

            if (port == default || ip == null || string.IsNullOrEmpty(nicknameBox.Text))
            {
                var incorrectFields =
                    $"{(port == default ? "port, " : string.Empty)}" +
                    $"{(ip == null ? "ip address, " : string.Empty)}" +
                    $"{(string.IsNullOrEmpty(nicknameBox.Text) ? "nickname" : string.Empty)}";

                MessageBox.Show($"Some fields are incorrect: {incorrectFields.TrimEnd(new []{' ', ','})}");
            }
            else
            {
                var fillChatDelegate       = new FillChatDelegate(AppendText);
                var updateUserListDelegate = new UpdateUserListDelegate(UpdateUserList);

                try
                {
                    _backgroundWorker.Connect(
                        ip.ToString(),
                        port,
                        nicknameBox.Text,
                        fillChatDelegate,
                        updateUserListDelegate);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }

                connectButton.Enabled    = false;
                nicknameBox.Enabled      = false;
                portTextBox.Enabled      = false;
                ipAddressTextBox.Enabled = false;
            }
        }
Beispiel #3
0
        ///<exception cref="SocketException"></exception>
        public void Connect(
            string hostname,
            int port,
            string nickname,
            FillChatDelegate fillChatDelegate,
            UpdateUserListDelegate updateUserListDelegate)
        {
            if (!_isConnected)
            {
                _nickname               = nickname;
                _fillChatDelegate       = fillChatDelegate;
                _updateUserListDelegate = updateUserListDelegate;

                _client      = new TcpClient(hostname, port);
                _stream      = _client.GetStream();
                _isConnected = true;

                SendMessage(new NetworkProtocol.Event(NetworkProtocol.EventType.Connect, _nickname));

                Task.Run(StartReceiveMessages);
            }
        }