/// <summary> /// 点击好友聊天方法 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void btnFriendChat_Click(object sender, EventArgs e) { Button btn = (Button)sender; if (btn.BackColor == Color.Gray) { return; } string str = (string)btn.Tag; //IPEndPoint chatIp = new IPEndPoint(IPAddress.Parse(str), 23333); // 判断集合里有没有此键,没有就创建一个,并发送一条请求通信消息 2协议 FriendChat showForm = null; if (!dic.ContainsKey(str)) { Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); byte b = 2; List <byte> list = new List <byte>(); list.Add(b); // 初始化聊天对象 JavaScriptSerializer js = new JavaScriptSerializer(); ChatContent cc = new ChatContent(); cc.SendIP = this.IP; cc.ChatType = 0; cc.Content = string.Empty; string strCc = js.Serialize(cc); list.AddRange(Encoding.UTF8.GetBytes(strCc)); IPEndPoint lastComIP = new IPEndPoint(IPAddress.Parse(str), 23333); // 向对方请求通信 s.SendTo(list.ToArray(), lastComIP); showForm = new FriendChat(str, this.IP); dic.Add(str, showForm); } else { showForm = dic[str]; } showForm.Show(); }
/// <summary> /// 接收消息方法 /// </summary> private void Receive() { try { // 用来存储获得的数据 byte[] receiveBytes = new byte[1024 * 1024]; // 用来存放接受数据的ip改变情况 EndPoint p = new IPEndPoint(IPAddress.Any, 23333); // 不停地循环监听收到的消息 while (true) { int i = listenSocket.ReceiveFrom(receiveBytes, ref p); if (receiveBytes[0] == 2)// 2代表请求通信,接收定义好的端口好,new出窗体对象,并隐藏 { // 反序列化 string content = Encoding.UTF8.GetString(receiveBytes, 1, i - 1); JavaScriptSerializer javass = new JavaScriptSerializer(); ChatContent cc = javass.Deserialize <ChatContent>(content); if (cc.ChatType == 0) { sp.SoundLocation = @"../../voices/system.wav"; sp.Play(); if (!dic.ContainsKey(cc.SendIP)) { FriendChat fc = new FriendChat(cc.SendIP, this.IP); fc.Hide(); dic.Add(cc.SendIP, fc); } } // 如果不可见,就不进行操作,防止线程问题 if (!dic[cc.SendIP].Visible) { continue; } if (cc.ChatType == 1) // 发的文字 { sp.SoundLocation = @"../../voices/msg.wav"; sp.Play(); FontSet(dic[cc.SendIP].rtbShow, cc.Content, Color.FromArgb(Convert.ToInt32(cc.FontColor))); dic[cc.SendIP].rtbShow.AppendText(cc.SendIP + ":" + cc.Content + "\r\n"); } else if (cc.ChatType == 2) // 发的窗口抖动 { for (int j = 0; j < 200; j++) { dic[cc.SendIP].Left += 30; dic[cc.SendIP].Top += 30; dic[cc.SendIP].Left -= 30; dic[cc.SendIP].Top -= 30; } } continue; } // 解析数据,获得IP,IP是自己的则跳出执行下一个循环 JavaScriptSerializer jss = new JavaScriptSerializer(); Friend fri = jss.Deserialize <Friend>(Encoding.UTF8.GetString(receiveBytes, 1, i - 1)); EndPoint requestIP = new IPEndPoint(IPAddress.Parse(fri.IP), 23333); if (fri.IP == this.IP) { continue; } // 判断发过来的消息是要干嘛 if (receiveBytes[0] == 0) // 0代表找好友,返回个人信息,并返回1协议 { // 播放声音提醒好友上线 sp.SoundLocation = @"../../voices/Global.wav"; sp.Play(); // 在集合中添加此在线用户 onlineList.Add(fri); // 调用下面的分析方法 Arrange(receiveBytes, i); List <byte> list = new List <byte>(); // 使用协议1 byte b = 1; list.Add(b); //list.AddRange(Encoding.UTF8.GetBytes(ip)); // 将此机器的IP地址发送给所有机器 //使用Json来序列化自己的信息 Friend f = new Friend(); f.IP = this.IP; f.Name = this.MyName; f.Photo = this.MyPhoto; f.Signature = this.Signature; JavaScriptSerializer js = new JavaScriptSerializer(); string strF = js.Serialize(f); list.AddRange(Encoding.UTF8.GetBytes(strF)); listenSocket.SendTo(list.ToArray(), requestIP); } else if (receiveBytes[0] == 1) // 1代表返回了好友信息,接收不再发广播 { // 播放声音提醒好友上线 sp.SoundLocation = @"../../voices/Global.wav"; sp.Play(); onlineList.Add(fri); Arrange(receiveBytes, i); } else if (receiveBytes[0] == 6)// 6表示此用户已下线 { onlineList.Remove(fri); UpdateFriendState(fri, false); } } } catch { } }