/// <summary> /// 文本消息包接收后处理 /// </summary> /// <param name="e"></param> void RecieveForTextMessage(PackageReceiveArgs e) { var msg = e.Message as TextMessage; if (null == msg) { return; } msg.MessageID = IDProvider.NewId(); msg.SendTime = DateTime.Now.ToDateTimeString(); //加入写入历史消息队列 QueueWriteToHistory.EnQueue(msg); IChannel channel = GetUserChannel(msg.ReceiverID); //如果消息接收者已连接 if (null != channel) { //发送消息给接收者 Server.Send(msg, channel); //放入发送队列 //QueueWaitSend.EnQueue(msg); } else { //加入未发送消息队列 QueueUnSendToDB.EnQueue(msg); //记录未发送消息标识 AddUnrecived(msg.ReceiverID, 1); } }
public void Import(byte[] data, int start, int count) { try { if (mCheckSize == null) { mCheckSize = new CheckSize(); } while (count > 0) { if (!mLoading) { mCheckSize.Reset(); mStream = new System.IO.MemoryStream(); mLoading = true; } if (mCheckSize.Length == -1) { while (count > 0 && mCheckSize.Length == -1) { mCheckSize.Import(data[start]); start++; count--; } } else { if (mCheckSize.Length > Application.PacketMaxsize) { Channel.Dispose(); "Data Overflow max length is {0}".ThrowError<Exception>(Application.PacketMaxsize); } if (OnImport(data, ref start, ref count)) { mLoading = false; if (this.Receive != null) { mStream.Position = 0; PackageReceiveArgs e = new PackageReceiveArgs(); e.Channel = this.Channel; e.Message = GetMessage(mStream); Receive(this, e); } } } } } catch (Exception e_) { throw e_; } }
/// <summary> /// 接收到消息处理 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void OnReceiveMessage(object sender, PackageReceiveArgs e) { if (e.Message is Login) { ReceiveForLogin(e); } else if (e.Message is TextMessage) { RecieveForTextMessage(e); } }
public void Import(byte[] data, int start, int count) { try { if (mCheckSize == null) { mCheckSize = new CheckSize(); } while (count > 0) { if (!mLoading) { mCheckSize.Reset(); mStream = new System.IO.MemoryStream(); mLoading = true; } if (mCheckSize.Length == -1) { while (count > 0 && mCheckSize.Length == -1) { mCheckSize.Import(data[start]); start++; count--; } } else { if (mCheckSize.Length > Application.PacketMaxsize) { Channel.Dispose(); "Data Overflow max length is {0}".ThrowError <Exception>(Application.PacketMaxsize); } if (OnImport(data, ref start, ref count)) { mLoading = false; if (this.Receive != null) { mStream.Position = 0; PackageReceiveArgs e = new PackageReceiveArgs(); e.Channel = this.Channel; e.Message = GetMessage(mStream); Receive(this, e); } } } } } catch (Exception e_) { throw e_; } }
private void OnReceive(object sender, PackageReceiveArgs e) { lock (this) { if (e.Message is Remoting.RPC.MethodResult) { Remoting.RPC.MethodResult result = (Remoting.RPC.MethodResult)e.Message; if (mRemotingMethods.TryGetValue(result.ID, out mMethodReturnArgs)) { mMethodReturnArgs.Import(result); } } else { if (mMethodReturnArgs == null) { if (Receive != null) { Receive(sender, e); } } else { if (mMethodReturnArgs.Status == InvokeStatus.Return) { mMethodReturnArgs.SetReturn(e.Message); } else if (mMethodReturnArgs.Status == InvokeStatus.Parameter) { mMethodReturnArgs.AddParameter(e.Message); } } } if (mMethodReturnArgs != null && mMethodReturnArgs.Status == InvokeStatus.Completed) { mMethodReturnArgs.Completed(); mMethodReturnArgs = null; } } }
private void OnReceiveMessage(object sender, PackageReceiveArgs e) { Application.MessageCenter.ProcessMessage(e.Message, GetSession(e.Channel)); }
private void OnReceiveMessage(object sender, PackageReceiveArgs e) { Application.MessageCenter.ProcessMessage(e.Message, GetSession(e.Channel)); }
/// <summary> /// 登录消息包接收后处理 /// </summary> /// <param name="e"></param> void ReceiveForLogin(PackageReceiveArgs e) { var login = e.Message as Login; if (null == login) { return; } login.LoginTime = DateTime.Now.ToDateTimeString(); //设置通道名称为UserID e.Channel.Name = login.UserID.ToString(); //写入登录队列处理 QueueLogin.EnQueue(login); #region 发送登录结果消息 //登录信息 var loginInfo = ServicesProvider.Items.UserService.GetUserLoginInfo(login.UserID); //如果登录信息不存在,则表明当前用户为首次登录 if (null == loginInfo) { loginInfo = new Td.Kylin.IM.Data.Model.UserLoginInfo { UserID = login.UserID, LastLoginAddress = login.AreaName, LastLoginTime = login.LoginTime.ToDateTime(), PrevLoginAddress = login.AreaName, PrevLoginTime = login.LoginTime.ToDateTime() }; } //如果获取的登录信息记录的最后登录时间与本次登录时间不一致,说明未更新到当前登录,则采用最后前置 else if ((loginInfo.LastLoginTime - login.LoginTime.ToDateTime()).TotalSeconds < 1) { loginInfo.PrevLoginAddress = loginInfo.LastLoginAddress; loginInfo.PrevLoginTime = loginInfo.LastLoginTime; loginInfo.LastLoginAddress = login.AreaName; loginInfo.LastLoginTime = login.LoginTime.ToDateTime(); } //登录结果回传 LoginPostBack backData = new LoginPostBack { Error = null, IsSuccess = true, LoginTime = login.LoginTime, PrevLoginAddress = loginInfo.PrevLoginAddress, PrevLoginTime = loginInfo.PrevLoginTime, UserID = login.UserID, UserName = login.UserName }; Server.Send(backData, e.Channel); #endregion #region 发送离线消息 int unreceivedCount = GetUnrecivedCount(login.UserID); //如果存在未接收的消息 if (unreceivedCount > 0) { //未接收成功的消息包集合 var unreceivedList = ServicesProvider.Items.UnsendMessageService.GetList(login.UserID); if (null != unreceivedList) { //放入待发送消息队列中 foreach (var m in unreceivedList) { var waitMsg = new TextMessage { Content = m.Content, MessageID = m.MessageID, MessageType = m.MessageType, ReceiverID = m.ReceiverID, ReceiverName = string.Empty, SenderID = m.SenderID, SendTime = m.SendTime.ToDateTimeString(), SernderName = m.SenderName }; QueueWaitSend.EnQueue(waitMsg); } //更新未接收数量标识信息 AddUnrecived(login.UserID, unreceivedList.Count()); //将本次回传的消息从未发送记录数据库中移除 long[] delIDs = unreceivedList.Select(p => p.MessageID).ToArray(); ServicesProvider.Items.UnsendMessageService.DeleteMessage(delIDs); } } #endregion }