/// <summary> /// 处理接收的客户端信息 /// </summary> /// <param name="userState">客户端信息</param> private void ReceiveData() { string receiveData = ""; while (IsExit == false) { try { receiveData = streamReader.ReadLine().Replace("@", "").Replace("\r\n", ""); //从网络流中读出字符串,此方法会自动判断字符串长度前缀 //receiveData = _BinaryReader.ReadString(); receiveData = Utils.DecodeBase64(receiveData); if (ReceiveSuccessed != null) { ReceiveSuccessed(JsonConvert.DeserializeObject <TextRequestInfo>(receiveData)); } Utils.SaveLog("receiveData", receiveData); } catch (Exception ex) { //if (IsExit == false) { Utils.SaveLog("断开与服务器的连接", ex.Message); } Utils.SaveLog("Client接收数据异常", ex.Message); ErrorRequestInfo info = new ErrorRequestInfo(); info.Content = "断线啦,请重连!"; ReceiveSuccessed(info); break; } } Utils.SaveLog("receiveData:", "服务器关闭:" + receiveData); }
/// <summary> /// 在listBox_Chats消息记录中追加信息 /// </summary> /// <param name="str">要追加的信息</param> private void AddChatsToList(IBaseRequestInfo baseInfo) { try { if (rtbMsgList.InvokeRequired) { AddItemToListBoxDelegate d = AddChatsToList; rtbMsgList.Invoke(d, baseInfo); } else { switch (baseInfo.MsgType) { case MsgType.Text: TextRequestInfo textInfo = (TextRequestInfo)baseInfo; rtbMsgList.SelectionStart = rtbMsgList.Text.Length; //设置插入符位置为文本框末 if (this.txtUserName.Text != textInfo.FromUserName) { rtbMsgList.SelectionColor = Color.Blue; //设置文本颜色 } else { rtbMsgList.SelectionColor = Color.Green; //设置文本颜色 } rtbMsgList.AppendText(textInfo.FromUserName + "(" + Utils.GetDateTime() + ")\r\n"); rtbMsgList.SelectionStart = rtbMsgList.Text.Length; //设置插入符位置为文本框末 rtbMsgList.SelectionColor = Color.Black; //设置文本颜色 rtbMsgList.AppendText(textInfo.Content + "\r\n"); break; case MsgType.Error: ErrorRequestInfo errorInfo = (ErrorRequestInfo)baseInfo; MessageBox.Show(this, errorInfo.Content); txtUserName.Enabled = true; this.btnLogin.Enabled = true; this.buttom_Send.Enabled = false; break; } } } catch { } }