/// <summary> /// 退出客户端 /// </summary> private void ExitApplication() { try { if (UnitySocket?.Connected ?? false) { //发送注销登录消息 UnitySocket.Send(Encoding.UTF8.GetBytes(ProtocolFormatter.FormatProtocol(ProtocolFormatter.CMDType.SignOut, Application.ProductVersion, UnityModule.USERID))); UnitySocket.Close(); } }catch {} try { ReceiveThread?.Abort(); } catch {} this.Close(); Application.Exit(); }
/// <summary> /// 发送消息 /// </summary> private void ChatSendButton_Click(object sender, EventArgs e) { if (UnitySocket == null) { //Socket为空,需要初始化 //初始化失败时需要结束 } if (!UnitySocket.Connected) { //Socket未连接,需要连接 //连接失败时需要结束 } if (FriendItem.ActiveFriend == null) { return; } if (string.IsNullOrEmpty(ChatInputTextBox.Text)) { return; } FriendsFlowPanel.Controls.SetChildIndex(FriendItem.ActiveFriend, 0); FriendItem.ActiveFriend.ChatBubblesPanel.Controls.Add( new ChatBubble ( "0", DateTime.Now.ToString(), UnityModule.USERID, ChatInputTextBox.Text, BubbleMaxWidth, true )); UnitySocket.Send(Encoding.UTF8.GetBytes(ProtocolFormatter.FormatProtocol(ProtocolFormatter.CMDType.ChatMessage, Application.ProductVersion, FriendItem.ActiveFriend.FriendID, ChatInputTextBox.Text))); ChatInputTextBox.Text = ""; }
/// <summary> /// 登录 /// </summary> private void Login() { //更新界面 this.Invoke(new Action(() => { UserIDTextBox.Enabled = false; PasswordTextBox.Enabled = false; SignInButton.Text = "Cancel"; this.Invalidate(); })); try { //创建TCP连接 LoginSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); LoginSocket.Connect(UnityModule.ServerIP, UnityModule.ServerPort); //发送登录协议 LoginSocket.Send(Encoding.ASCII.GetBytes(ProtocolFormatter.FormatProtocol(ProtocolFormatter.CMDType.SignIn, Application.ProductVersion, UserIDTextBox.Text, PasswordTextBox.Text))); //接收登录验证结果 byte[] SignResultBytes = new byte[LoginSocket.ReceiveBufferSize - 1]; LoginSocket.Receive(SignResultBytes); string SignResult = Encoding.ASCII.GetString(SignResultBytes).Trim('\0'); UnityModule.DebugPrint("接收到登录结果:{0}", SignResult); string MessagePattern = ProtocolFormatter.GetCMDTypePattern(); Regex MessageRegex = new Regex(MessagePattern, RegexOptions.IgnoreCase | RegexOptions.Singleline); Match MessageMatchResult = MessageRegex.Match(SignResult); string cmdType = MessageMatchResult.Groups["CMDTYPE"].Value.ToUpper(); switch (cmdType) { case "SIGNINSUCCESSFULLY": { //登录成功,切换界面 this.Invoke(new Action(() => { ShowTips("登录成功!"); SignInButton.Text = "Success."; this.Invalidate(); UnityModule.USERID = UserIDTextBox.Text; new ClientForm() { loginForm = this }.Show(); HideMe(HideTo.JustHide); })); break; } case "SIGNINUNSUCCESSFULLY": { //登录失败 this.Invoke(new Action(() => { ShowTips("您的密码输入错误,请重试!"); SignInButton.Text = "Sign In"; PasswordTextBox.Focus(); })); break; } } LoginSocket?.Close(); UnityModule.DebugPrint("验证登录TCP连接已经关闭 ..."); UserIDTextBox.Enabled = true; PasswordTextBox.Enabled = true; this.Invalidate(); } catch (ThreadAbortException) { //遇到线程中断异常时忽略,因为此异常由用户手动取消放弃登录任务。 return; } catch (Exception ex) { //登录过程中遇到错误 Logining = false; UnityModule.DebugPrint("登录遇到错误!{0}", ex.Message); this.Invoke(new Action(() => { UserIDTextBox.Enabled = true; PasswordTextBox.Enabled = true; SignInButton.Text = "Sign In"; this.Invalidate(); ShowTips("登录遇到错误,请检查网络连接。" + Convert.ToString(ex.HResult, 16)); })); } Logining = false; }
/// <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); } } } }