コード例 #1
0
 /// <summary>
 /// 在对应的FrmChat窗体对象内显示接收的TCP数据
 /// </summary>
 public void showDialog(FrmChat frmChat, DataPack dataPack)
 {
     if (frmChat.flowLP_Chat.InvokeRequired == false) //如果调用该函数的线程和控件flowLP_Chat位于同一个线程内
     {
         frmChat.addLabel(dataPack._sender, dataPack._time, dataPack._message);
     }
     else //如果调用该函数的线程和控件flowLP_Chat不在同一个线程
     {
         //通过使用Invoke的方法,让子线程告诉窗体线程来完成相应的控件操作
         showDialogDelegate deleg = new showDialogDelegate(showDialog);
         //使用控件flowLP_Chat的Invoke方法执行deleg代理
         frmChat.flowLP_Chat.Invoke(deleg, frmChat, dataPack);
     }
 }
コード例 #2
0
ファイル: FrmMain.cs プロジェクト: W-Kinlon/WithMe
 public void receiveThread()
 {
     while (true)
     {
         byte[] buffer = new byte[1024];
         tcpClient.Receive(buffer, 0, buffer.Length, SocketFlags.None);
         DataPack dataPack = FrmChat.BytesToDataPack(buffer);
         if (frmChats.ContainsKey(dataPack._sender))
         {
             //获取发送者的对应聊天窗体
             FrmChat frmChat = FrmMain.frmChats[dataPack._sender];
             //在对应的FrmChat窗体对象内显示接收的TCP数据
             frmChat.showDialog(frmChat, dataPack);
         }
         else
         {
             lastMsg(dataPack);
         }
     }
 }
コード例 #3
0
ファイル: FrmMain.cs プロジェクト: W-Kinlon/WithMe
        private void FrmMain_Load(object sender, EventArgs e)
        {
            //初始化全局变量
            mysqlDB      = new MysqlDB();
            dicChatLists = new Dictionary <string, MessageList>();
            dicChats     = new Dictionary <string, List <OneWord> >();
            frmChats     = new Dictionary <string, FrmChat>();


            labName.Text = User.Name;//从类的静态字段中取值
            //从服务器拉取所有与本用户相关的聊天记录,存储到聊天记录字典
            //sql语句(谨慎使用drop,delete!!!)
            string sql = "SELECT * FROM qqchatView WHERE rname='" + loginName + "' OR sname='" + loginName + "' ORDER BY time ASC";

            //接口sql=SELECT * FROM qqchatView WHERE rname='yiki' OR sname='yiki' ORDER BY time ASC

            res = mysqlDB.Get(sql);
            while (res.Read())
            {
                string   _sender   = res.GetString("sname");; //发送者
                string   _receiver = res.GetString("rname");  //接收者
                DateTime _time     = res.GetDateTime("time"); //发送时间
                //_time = _time.AddHours(-8);    //加时间(时区转换)
                string  _text = res.GetString("text");        //发送内容
                string  _friend;                              //该条记录的好友昵称
                OneWord word = new OneWord();
                word.Time = _time;
                word.Text = _text;
                if (_sender.Equals(loginName))
                {
                    word.Sender = true;
                    _friend     = _receiver;
                }
                else
                {
                    word.Sender = false;
                    _friend     = _sender;
                }
                if (DicChats.ContainsKey(_friend))
                {
                    DicChats[_friend].Add(word);//该好友目录下增加一条聊天记录
                }
                else
                {//增加一条好友目录
                    List <OneWord> words = new List <OneWord>();
                    words.Add(word);
                    DicChats.Add(_friend, words);
                }
            }

            //将最新一条聊天记录显示到聊天记录列表控件中
            //反向遍历数组
            for (int i = DicChats.Count() - 1; i >= 0; i--)
            {
                var         data        = DicChats.ToArray()[i];
                MessageList messageList = new MessageList();
                messageList.ContactsName    = data.Key.ToString();
                messageList.LastMsg         = data.Value.Last().Text;
                messageList.LastMsgSendTime = data.Value.Last().Time.TimeOfDay.ToString();
                flowLayoutPanelMessage.Controls.Add(messageList);
                dicChatLists.Add(messageList.ContactsName, messageList);
            }
            //创建TCP套接字,发起连接请求
            try
            {
                tcpClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPAddress ipaddress = IPAddress.Parse(serverAdress);
                EndPoint  point     = new IPEndPoint(ipaddress, serverPoint);
                tcpClient.Connect(point); //通过IP和端口号,定位一个要连接到的服务端
                                          //向服务端发送初始化消息以表明自己身份
                DataPack dataPack = new DataPack(loginName, "server", "server", "server");
                tcpClient.Send(FrmChat.DataPackToBytes(dataPack));
                //创建线程,一直接收服务端发来的数据
                ThreadStart _receiveThread = new ThreadStart(receiveThread);
                Thread      _childThread   = new Thread(_receiveThread);
                _childThread.Start();
            }
            catch (Exception) {
                MessageBox.Show("服务器无响应,请稍后再试");
                System.Environment.Exit(0);
            }
        }