Beispiel #1
0
 public void openChat(Friend f,string msg)
 {
     FrmChat frm = new FrmChat(f);
     f.Frmchatting = frm;
     frm.Show();
     f.IsOpen = true;
     f.Frmchatting.txtChattingContent.AppendText(f.Nickname + ":" + msg + "\r\n");
 }
Beispiel #2
0
 public void addUcf(Friend f)
 {
     UCFriend ucf = new UCFriend();
     ucf.Frm = this;
     ucf.CurFriend = f;
     ucf.Top = this.plFriendsList.Controls.Count * ucf.Height;
     ucf.myDBClick += ucf_myDBClick;
     this.plFriendsList.Controls.Add(ucf);
     this.onlineFriends.Add(ucf.CurFriend);
 }
Beispiel #3
0
 public FrmChat(Friend f )
 {
     InitializeComponent();
     this.ChatFriend=f;
 }
Beispiel #4
0
        public void listen()
        {
            UdpClient uc = new UdpClient(9527);
            while (true)
            {
                IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 0);
                byte[] bmsg = uc.Receive(ref ipep);
                string msg = Encoding.Default.GetString(bmsg);
                string[] datas = msg.Split('|');
                string messageHead = datas[0];
                switch (messageHead)
                {
                    case "LOGIN":
                        if (datas.Length != 4)
                        {
                            continue;
                        }
                        Friend friend = new Friend();
                        int curIndex = Convert.ToInt32(datas[2]);
                        if (curIndex < 0 || curIndex >= _frm.ilHeaderImage.Images.Count)
                        {
                            curIndex = 0;
                        }
                        friend.HeaderImageIndex = curIndex;
                        friend.Nickname = datas[1];
                        friend.ShuoShuo = datas[3];
                        friend.IP = ipep.Address;
                        friend.IsOpen = false;
                        IPAddress myIp = Operation.getMyip();
                        if (myIp.ToString() != friend.IP.ToString())
                        {
                            _frm.Invoke(new delAddFriend(_frm.addUcf), friend);

                            //回发,告诉对方我也在

                            UdpClient onuc = new UdpClient();
                            string myName = "王凯";
                            string onmsg = "ALSOON|" + myName + "|13|大家好";
                            byte[] onbmsg = Encoding.Default.GetBytes(onmsg);
                            onuc.Send(onbmsg, onbmsg.Length, new IPEndPoint(ipep.Address, 9527));
                        }
                         break;

                    case "ALSOON":
                        if (datas.Length != 4)
                        {
                            continue;
                        }
                        Friend onfriend = new Friend();
                        int oncurIndex = Convert.ToInt32(datas[2]);
                        if (oncurIndex < 0 || oncurIndex >= _frm.ilHeaderImage.Images.Count)
                        {
                            oncurIndex = 0;
                        }
                        onfriend.HeaderImageIndex = oncurIndex;
                        onfriend.Nickname = datas[1];
                        onfriend.ShuoShuo = datas[3];
                        onfriend.IP = ipep.Address;
                        _frm.Invoke(new delAddFriend(_frm.addUcf), onfriend);
                        break;
                    case "LOGOUT":
                        Panel pnlst = _frm.getPanl();
                        int deleIndex = 0;
                        //根据当前下线人的ip地址,找到pn中对应的用户控件,删除;
                        foreach (UCFriend ltUcf in pnlst.Controls)
                        {
                            if (ltUcf.CurFriend.IP.ToString() == ipep.Address.ToString())
                            {
                                pnlst.Controls.Remove(ltUcf);
                                break;
                            }
                            deleIndex++;
                        }
                        //让其下面的每一个用户控件对象依次上移
                        for (int i = deleIndex; i < pnlst.Controls.Count; i++)
                        {
                            pnlst.Controls[i].Top = i * pnlst.Controls[0].Height;
                        }
                        break;
                    case "MSG":
                        if (datas.Length != 3)
                        {
                            continue;
                        }
                        foreach (Friend olucf  in _frm.onlineFriends)
                        {
                            if(ipep.Address.ToString()==olucf.IP.ToString())
                            {
                                if (olucf.IsOpen == true)
                                {
                                    olucf.Frmchatting.txtChattingContent.AppendText(datas[2] + ":" + datas[1]+"\r\n");
                                }
                                if(olucf.IsOpen==false)
                                {
                                    object[] openPars = new object[2];
                                    openPars[0] = olucf;
                                    openPars[1] = datas[1];
                                    _frm.Invoke(new openChat(_frm.openChat),openPars);
                                 }
                                break;
                            }

                        }
                        break;
                    default:
                        break;
                }
            }
        }