/// <summary> /// 动态隐藏方法 /// </summary> /// <param name="hideTo">隐藏后的动作</param> private void HideMe(HideTo hideTo) { UnityModule.DebugPrint("开始动态隐藏窗体...{0}", this.Name); ThreadPool.QueueUserWorkItem(new WaitCallback(delegate { int IniTop = this.Top; while (this.Opacity > 0) { this.Opacity -= 0.1; this.Top -= 2; Thread.Sleep(15); } if (hideTo == HideTo.Min) { UnityModule.DebugPrint("最小化窗体"); this.WindowState = FormWindowState.Minimized; this.Opacity = 1.0; this.Top = IniTop; } else if (hideTo == HideTo.JusetClose) { UnityModule.DebugPrint("关闭ClientForm"); //初始化FriendItem静态数据,否则再次登录后会出现问题 FriendItem.ResetStaticData(); AllowToClose = true; this.Close(); } else if (hideTo == HideTo.ExitApp) { UnityModule.DebugPrint("退出程序"); AllowToClose = true; ExitApplication(); } })); }
/// <summary> /// FriendItem激活项改变,切换聊天记录控件 /// </summary> /// <param name="OldActiveItem"></param> /// <param name="NewActiveItem"></param> private void FriendItemActiveChanged(object OldActiveItem, FriendItem NewActiveItem) { if ((FriendItem)OldActiveItem != null) { UnityModule.DebugPrint("旧的激活项:{0}", ((FriendItem)OldActiveItem).FriendID); ((FriendItem)OldActiveItem).ChatDraft = ChatInputTextBox.Text; ((FriendItem)OldActiveItem).ChatBubblesPanel?.Hide(); } if ((FriendItem)NewActiveItem != null) { UnityModule.DebugPrint("新的激活项:{0}", ((FriendItem)NewActiveItem).FriendID); if (!ChatSendPanel.Visible) { ChatSendPanel.Show(); } ChatInputTextBox.Text = NewActiveItem.ChatDraft; if (NewActiveItem.ChatBubblesPanel != null) { NewActiveItem.ChatBubblesPanel.Show(); NewActiveItem.ChatBubblesPanel.BringToFront(); } } else { ChatSendPanel.Hide(); } }
/// <summary> /// 接收来自服务器的消息 /// </summary> private void ReceiveMessage() { while (true) { #region 接收消息 byte[] MessageBuffer = new byte[] { }; int MessageBufferSize = 0; string ServerMessagePackage = ""; try { MessageBuffer = new byte[UnitySocket.ReceiveBufferSize - 1]; MessageBufferSize = UnitySocket.Receive(MessageBuffer); ServerMessagePackage = Encoding.UTF8.GetString(MessageBuffer, 0, MessageBufferSize); } catch (ThreadAbortException) { return; } catch (Exception ex) { UnityModule.DebugPrint("接收消息时遇到错误:{0}", ex.Message); HideMe(HideTo.JusetClose); this.loginForm.Show(); this.loginForm.ShowTips("与服务器连接中断,请检查网络连接。" + Convert.ToString(ex.HResult, 16)); return; } UnityModule.DebugPrint("ServerMessagePackage : {0}", ServerMessagePackage); #endregion /* * 遇到严重的TCP粘包问题,服务端分多次发送的GETFRIENDSLIST协议,被一次发送给了客户端 * 导致客户端只能提取到第一条协议,因为每条协议以'\n'结尾,所以每次收到数据包后对其按'\n'分割 * 分割后判断不为空的段后加'\n'再按正常的协议包处理,为空不处理 * 以上,解决粘包问题 */ string[] ServerMessages = ServerMessagePackage.Split('\n'); foreach (string TempServerMessage in ServerMessages) { try { if (string.IsNullOrEmpty(TempServerMessage)) { continue; } #region 读取消息协议类型 string ServerMessage = TempServerMessage + '\n'; UnityModule.DebugPrint("ServerMessage : {0}", ServerMessage); string MessagePattern = ProtocolFormatter.GetCMDTypePattern(); Regex MessageRegex = new Regex(MessagePattern, RegexOptions.IgnoreCase | RegexOptions.Singleline); Match MessageMatchResult = MessageRegex.Match(ServerMessage); string cmdType = MessageMatchResult.Groups["CMDTYPE"].Value.ToUpper(); UnityModule.DebugPrint("收到 CMDTYPE : {0}", cmdType); #endregion switch (cmdType) { case "CHATMESSAGE": { #region 聊天消息 string FromID = null, Message = null; int MessageID; DateTime ChatTime; MessagePattern = ProtocolFormatter.GetProtocolPattern(ProtocolFormatter.CMDType.ChatMessage); MessageRegex = new Regex(MessagePattern, RegexOptions.IgnoreCase | RegexOptions.Singleline); MessageMatchResult = MessageRegex.Match(ServerMessage); FromID = MessageMatchResult.Groups["FROMID"].Value; ChatTime = DateTime.TryParse(MessageMatchResult.Groups["CHATTIME"].Value.ToString(), out ChatTime) ? ChatTime.ToLocalTime() : DateTime.Now; MessageID = int.Parse(MessageMatchResult.Groups["MESSAGEID"].Value); Message = Encoding.UTF8.GetString(Convert.FromBase64String(MessageMatchResult.Groups["MESSAGE"].Value)); this.Invoke(new Action(() => { FriendItem MessageFrom = FriendItem.GetFriendItemByFriendID(FromID); if (MessageFrom != null) { FriendsFlowPanel.Controls.SetChildIndex(MessageFrom, 0); if (FriendItem.ActiveFriend != MessageFrom) { MessageFrom.MessageNRTCount += 1; } MessageFrom.ChatBubblesPanel.Controls.Add( new ChatBubble ( MessageID.ToString(), ChatTime.ToString(), FromID, Message, BubbleMaxWidth, false ) ); } })); break; #endregion } case "GETCHATHISTORY": { #region 获取历史聊天记录 //todo:显示历史聊天记录 string FromID = ""; int MessageID = 0; //更新本地第一条聊天记录MessageID if (FriendsFirstMessageID.ContainsKey(FromID)) { //如果遇到更小的MessageID,只有 if (MessageID < FriendsFirstMessageID[FromID]) { FriendsFirstMessageID[FromID] = MessageID; } } else { //估计运行不到这里 FriendsFirstMessageID.Add(FromID, MessageID); } break; #endregion } case "GETFRIENDSLIST": { #region 获取好友列表 string FriendID = null, NickName = null, Signature = null; bool OnLine = false; MessagePattern = ProtocolFormatter.GetProtocolPattern(ProtocolFormatter.CMDType.GetFriendsList); MessageRegex = new Regex(MessagePattern, RegexOptions.IgnoreCase | RegexOptions.Singleline); MessageMatchResult = MessageRegex.Match(ServerMessage); FriendID = MessageMatchResult.Groups["FRIENDID"].Value.ToString(); NickName = Encoding.UTF8.GetString(Convert.FromBase64String(MessageMatchResult.Groups["NICKNAME"].Value.ToString())); Signature = Encoding.UTF8.GetString(Convert.FromBase64String(MessageMatchResult.Groups["SIGNATURE"].Value.ToString())); OnLine = Convert.ToBoolean(MessageMatchResult.Groups["ONLINE"].Value.ToString()); this.Invoke(new Action(() => { UnityModule.DebugPrint("收到好友信息:{1} ({0}):{2}", FriendID, NickName, Signature); if (FriendItem.FriendExisted(FriendID)) { //TODO:如果 FriendID已经存在,且FriendItem不为null,仅更新FriendID的信息,此特性可以在服务端用于有用户更新了资料时,立即向好友客户端更新资料 FriendItem.GetFriendItemByFriendID(FriendID)?.SetNickNameSignatureAndOnLine(NickName, Signature, OnLine); } else { //默认好友聊天历史记录最早一条MessageID=0 if (!FriendsFirstMessageID.ContainsKey(FriendID)) { FriendsFirstMessageID.Add(FriendID, 0); } //新添加 FriendItem FriendItem NewFriendItem = new FriendItem(FriendID, NickName, Signature, OnLine) { RightToLeft = RightToLeft.No }; NewFriendItem.FriendItemClick += new EventHandler(FriendItemClick); //创建好友聊天记录控件 MyTableLayoutPanel NewChatBubblePanel = new MyTableLayoutPanel() { AutoScroll = true, Dock = DockStyle.Fill, BackColor = Color.White, Visible = false, }; //绑定自动滚动到底部事件 NewChatBubblePanel.ControlAdded += new ControlEventHandler(ChatBubblesPanel_ControlAdded); MainPanel.Controls.Add(NewChatBubblePanel); NewFriendItem.ChatBubblesPanel = NewChatBubblePanel; FriendsFlowPanel.Controls.Add(NewFriendItem); } })); break; #endregion } case "FRIENDSIGNIN": { #region 好友登录 string FriendID = null; MessagePattern = ProtocolFormatter.GetProtocolPattern(ProtocolFormatter.CMDType.FriendSignIn); MessageRegex = new Regex(MessagePattern, RegexOptions.IgnoreCase | RegexOptions.Singleline); MessageMatchResult = MessageRegex.Match(ServerMessage); FriendID = MessageMatchResult.Groups["FRIENDID"].Value.ToString(); this.Invoke(new Action(() => { UnityModule.DebugPrint("收到好友登录消息:{0}", FriendID); if (FriendItem.FriendExisted(FriendID)) { FriendItem JustSignIn = FriendItem.GetFriendItemByFriendID(FriendID); if (JustSignIn != null) { FriendsFlowPanel.Controls.SetChildIndex(JustSignIn, FriendItem.OnLineCount); JustSignIn.OnLine = true; } } })); break; #endregion } case "FRIENDSIGNOUT": { #region 好友注销登录 string FriendID = null; MessagePattern = ProtocolFormatter.GetProtocolPattern(ProtocolFormatter.CMDType.FriendSignOut); MessageRegex = new Regex(MessagePattern, RegexOptions.IgnoreCase | RegexOptions.Singleline); MessageMatchResult = MessageRegex.Match(ServerMessage); FriendID = MessageMatchResult.Groups["FRIENDID"].Value.ToString(); this.Invoke(new Action(() => { UnityModule.DebugPrint("收到好友注销消息:{0}", FriendID); if (FriendItem.FriendExisted(FriendID)) { FriendItem JustSignIn = FriendItem.GetFriendItemByFriendID(FriendID); if (JustSignIn != null) { JustSignIn.OnLine = false; FriendsFlowPanel.Controls.SetChildIndex(JustSignIn, FriendItem.OnLineCount); } } })); break; #endregion } case "FRIENDSLISTCOMPLETE": { #region 好友列表获取完毕 UnitySocket.Send(Encoding.UTF8.GetBytes(ProtocolFormatter.FormatProtocol(ProtocolFormatter.CMDType.GetMessageNotSendYet, Application.ProductVersion, UnityModule.USERID))); break; #endregion } case "MESSAGENSYCOMPLETE": { #region 未读消息获取完毕 //准备完毕,测试用 UnitySocket.Send(Encoding.UTF8.GetBytes(ProtocolFormatter.FormatProtocol(ProtocolFormatter.CMDType.GetChatHistory, Application.ProductVersion, "66666", "0"))); break; #endregion } case "ANOTHORSIGNIN": { #region 异地登录 this.Invoke(new Action(() => { HideMe(HideTo.JusetClose); this.loginForm.Show(); this.loginForm.ShowTips("您的账号异地登陆,请注意密码安全!"); UnitySocket.Close(); ReceiveThread.Abort(); })); //这里需要 return; 否则会进入 catch(){} 被当做异常处理 return; #endregion } case "SERVERSHUTDOWN": { #region 远程服务端关闭 this.Invoke(new Action(() => { HideMe(HideTo.JusetClose); this.loginForm.Show(); this.loginForm.ShowTips("远程服务器主动关闭,可能Leon关机去上课了..."); UnitySocket.Close(); ReceiveThread.Abort(); })); return; #endregion } default: { #region 未知的消息协议 this.Invoke(new Action(() => { new MyMessageBox("遇到未知的 CMDTYPE : " + cmdType, MyMessageBox.IconType.Info).Show(this); })); break; #endregion } } } catch (ThreadAbortException) { return; } catch (Exception ex) { UnityModule.DebugPrint("处理消息时遇到错误:{0}", ex.Message); } } } }