public override async Task <ResponseModel> Act(string message, ChatSession session) { var result = new ResponseModel(); IResponseInfo responseInfo; if (session.WordSequence == null) { StartRequestInfo info = new StartRequestInfo { Request = new StartRequest(session) }; responseInfo = await _logic.StartChat(info); } else { TextRequestInfo info = new TextRequestInfo { Request = new TextRequest(session, message) }; responseInfo = await _logic.HandleText(info); } result.Message = responseInfo.Message; result.Session = session; result.Success = responseInfo.ResponseCode == ResponseCodes.OK; return(result); }
/// <summary> /// 发送指令 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button_Send_Click(object sender, EventArgs e) { if (socketServer == null) { return; } TextRequestInfo info = new TextRequestInfo(); info.FromUserName = this.txtUserName.Text; info.ToUserName = "******"; info.Content = this.txtContent.Text; info.MsgType = MsgType.Text; socketServer.SendMessage(Utils.EncodeBase64(JsonConvert.SerializeObject(info))); AddChatsToList(info); this.txtContent.Text = ""; }
/// <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 { } }
/// <summary> /// 文本消息处理 /// </summary> /// <param name="baseInfo"></param> public void Text_RequestHandler(ChatsSession session, IBaseRequestInfo baseInfo) { TextRequestInfo info = (TextRequestInfo)baseInfo; session.UserName = info.FromUserName; #region 显示请求信息 StringBuilder sb = new StringBuilder(); sb.Append(string.Format("-------------start({0})-----------------\r\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))); sb.Append(string.Format("发送人:{0}\r\n时间:{1}\r\n消息内容:{2}", info.FromUserName, info.CreateTime, info.Content)); sb.Append("\r\n-------------end-----------------\r\n"); ShowMsg(sb.ToString()); foreach (ChatsSession s in GetAllChatsSession()) { if (s.UserName != info.FromUserName) { SessionHelper.SendMessage(s, JsonConvert.SerializeObject(info)); } } #endregion }