Exemple #1
0
 public void lastMsg(DataPack dataPack)
 {
     //保存接收的信息到字典,刷新聊天列表并发出提示音
     if (dataPack._time != "")
     {
         OneWord word = new OneWord();
         word.Time   = Convert.ToDateTime(dataPack._time);
         word.Text   = dataPack._message;
         word.Sender = false;
         if (DicChats.ContainsKey(dataPack._sender))
         {
             DicChats[dataPack._sender].Add(word); //该好友目录下增加一条聊天记录
                                                   //刷新消息
             dicChatLists[dataPack._sender].refresh(dicChatLists[dataPack._sender], dataPack._time, dataPack._message);
         }
         else
         {
             List <OneWord> words = new List <OneWord>();
             words.Add(word);
             DicChats.Add(dataPack._sender, words);//增加一条好友目录
             MessageList messageList = new MessageList();
             messageList.ContactsName    = dataPack._sender;
             messageList.LastMsg         = dataPack._message;
             messageList.LastMsgSendTime = dataPack._time;
             flowLayoutPanelMessage.Controls.Add(messageList);
             dicChatLists.Add(messageList.ContactsName, messageList);
             messageList.refresh(messageList, dataPack._time, dataPack._message);
         }
     }
 }
Exemple #2
0
        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);
            }
        }