Example #1
0
        private void WindowLoaded(object sender, RoutedEventArgs e)
        {
            userNameLabel.Content = _userName;
            _clientInstance       = TcpHandler.GetInstance();

            //获取当前登录用户的图标
            var imagePath = "pack://application:,,,/images/" + _userName + ".jpg";
            var iamgeUri  = new Uri(imagePath);

            try
            {
                loginUserImage.Source = new BitmapImage(iamgeUri);
            }
            catch
            {
                imagePath             = "pack://application:,,,/images/user.jpg";
                iamgeUri              = new Uri(imagePath);
                loginUserImage.Source = new BitmapImage(iamgeUri);
            }
            //获取好友列表
            var commond = TcpHelper.PackCommmond(_userName, TcpHelper.TalkCommond.UpdateUserList);

            _clientInstance.SendMessage(commond);

            //新建线程接收服务端数据
            th1 = new Thread(ReceiveData);
            th1.Start();

            //新建线程,用于更新消息提示
            th2 = new Thread(ScanEmptyMsgTip);
            th2.Start();
        }
Example #2
0
        /// <summary>
        /// 登录连接到服务器
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void loginButton_Click(object sender, RoutedEventArgs e)
        {
            //获取服务器IP
            var serverIP = serverIPTextBox.Text;

            if (string.IsNullOrEmpty(serverIP))
            {
                MessageBox.Show("请填写正确的服务器地址");
                return;
            }
            try
            {
                var serverIPs = serverIP.Split(':');
                TcpHandler._serverHostName = serverIPs[0]; ToString();
                TcpHandler._serverPort     = System.Convert.ToInt32(serverIPs[1]);
            }catch (Exception ex) {
                MessageBox.Show("服务器地址格式不正确");
                return;
            }

            var userName = userNameTextBox.Text;

            if (string.IsNullOrEmpty(userName))
            {
                MessageBox.Show("请输入用户名");
                return;
            }


            try
            {
                clientInstance = TcpHandler.GetInstance();
                var sendMsg = TcpHelper.PackCommmond(userName, TcpHelper.TalkCommond.Login);
                //发送登录命令
                clientInstance.SendMessage(sendMsg);

                //创建新线程接收服务端消息
                Thread th = new Thread(ReceiveData);
                th.Start();
            }catch (Exception ex) {
                MessageBox.Show(ex.Message);
            }
        }
Example #3
0
        private void WindowLoaded(object sender, RoutedEventArgs e)
        {
            talkToNameLabel.Content = _talktoUserName;
            _clientInstance         = TcpHandler.GetInstance();

            //获取聊天对象的图标头像
            var imagePath = "pack://application:,,,/images/" + _talktoUserName + ".jpg";
            var iamgeUri  = new Uri(imagePath);

            try
            {
                talkToUserImage.Source = new BitmapImage(iamgeUri);
            }
            catch
            {
                imagePath = "pack://application:,,,/images/user.jpg";
                iamgeUri  = new Uri(imagePath);
                talkToUserImage.Source = new BitmapImage(iamgeUri);
            }

            //创建新线程用于读取聊天消息
            _readMessageTh = new Thread(ReadMessageList);
            _readMessageTh.Start();
        }