コード例 #1
0
    public static bool MakeWebRequest(LoginPostBack data)
    {
        string DynamicCMSMoblinUtilsKey = string.Empty,
               LoginPostBackEnabled     = string.Empty,
               LoginPostBackURL         = string.Empty;

        try
        {
            DynamicCMSMoblinUtilsKey = UtilsConfig.Get(enumConfigKeys.DynamicCMSMoblinUtilsKey);
            LoginPostBackEnabled     = UtilsConfig.Get(enumConfigKeys.LoginPostBackEnabled);
            LoginPostBackURL         = UtilsConfig.Get(enumConfigKeys.LoginPostBackURL);

            if (string.IsNullOrEmpty(DynamicCMSMoblinUtilsKey) ||
                string.IsNullOrEmpty(LoginPostBackEnabled) ||
                string.IsNullOrEmpty(LoginPostBackURL) ||
                !LoginPostBackEnabled.ToLower().Equals("true"))
            {
                return(false);
            }
        }
        catch (Exception ex)
        {
            return(false);
        }

        data.AppKey     = DynamicCMSMoblinUtilsKey;
        data.AppVersion = UtilsConfig.Get(enumConfigKeys.VERSION);
        data.IP         = GetIPAddress();

        try
        {
            HttpWebRequest oHttpRequest = (HttpWebRequest)WebRequest.Create(LoginPostBackURL);
            oHttpRequest.Headers.Clear();
            oHttpRequest.KeepAlive = false;
            oHttpRequest.Method    = "GET";
            if (data != null)
            {
                // Set values for the request back
                oHttpRequest.Method      = "POST";
                oHttpRequest.ContentType = "application/json";
                byte[] dataArr = Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(data));
                oHttpRequest.ContentLength = dataArr.Length;
                using (Stream stream = oHttpRequest.GetRequestStream())
                    for (int i = 0, lim = dataArr.Length; i < lim; i++)
                    {
                        stream.WriteByte(dataArr[i]);
                    }
            }
            HttpWebResponse oHttpResponse = (HttpWebResponse)oHttpRequest.GetResponse();
            //check status code
            //if (oHttpResponse.StatusCode == HttpStatusCode.OK)
            //    data = new StreamReader(oHttpResponse.GetResponseStream(), Encoding.UTF8).ReadToEnd();
        }
        catch (Exception ex)
        {
            return(false);
        }
        return(true);
    }
コード例 #2
0
        /// <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
        }