Exemple #1
0
        //LongPolling Action 1 - 处理客户端发起的请求
        public void LongPollingAsync()
        {
            var state = Request["state"];
            //计时器,5秒种触发一次Elapsed事件
            var timer = new System.Timers.Timer(1000);

            //告诉ASP.NET接下来将进行一个异步操作
            AsyncManager.OutstandingOperations.Increment();
            //订阅计时器的Elapsed事件
            timer.Elapsed += (sender, e) =>
            {
                //保存将要传递给LongPollingCompleted的参数
                AsyncManager.Parameters["model"] = new LoginLogModel();
                if (state != null)
                {
                    var log = _loginLogService.GetLoginLog(state);
                    if (log != null && log.IsDeleted == false)
                    {
                        //保存将要传递给LongPollingCompleted的参数
                        AsyncManager.Parameters["model"] = new LoginLogModel
                        {
                            Id         = log.Id,
                            CreateTime = log.CreateTime,
                            State      = log.State
                        };
                    }
                }
                //告诉ASP.NET异步操作已完成,进行LongPollingCompleted方法的调用
                AsyncManager.OutstandingOperations.Decrement();
            };
            //启动计时器
            timer.Start();
        }
        public object Post(LoginLogModel model)
        {
            if (model.WeChatUserModel.OpenId != null)
            {
                var wecharuser = _weChatUserService.GetWeChatUser(model.WeChatUserModel.OpenId);
                if (wecharuser == null)
                {
                    wecharuser = new WeChatUser
                    {
                        Id         = Guid.NewGuid(),
                        OpenId     = model.WeChatUserModel.OpenId,
                        NickName   = model.WeChatUserModel.NickName,
                        Gender     = (Gender)model.WeChatUserModel.Gender,
                        Language   = model.WeChatUserModel.Language,
                        City       = model.WeChatUserModel.City,
                        Province   = model.WeChatUserModel.Province,
                        Headimgurl = model.WeChatUserModel.Headimgurl
                    };
                    _weChatUserService.Insert(wecharuser);
                }
                else
                {
                    wecharuser.OpenId     = model.WeChatUserModel.OpenId;
                    wecharuser.NickName   = model.WeChatUserModel.NickName;
                    wecharuser.Gender     = (Gender)model.WeChatUserModel.Gender;
                    wecharuser.Language   = model.WeChatUserModel.Language;
                    wecharuser.City       = model.WeChatUserModel.City;
                    wecharuser.Province   = model.WeChatUserModel.Province;
                    wecharuser.Headimgurl = model.WeChatUserModel.Headimgurl;
                    _weChatUserService.Update();
                }
                var loginLogId = Guid.NewGuid();
                if (_loginLogService.GetLoginLog(model.State) == null)
                {
                    _loginLogService.Insert(new LoginLog
                    {
                        Id            = loginLogId,
                        State         = model.State,
                        CreateTime    = DateTime.Now,
                        WeChatUser_Id = wecharuser.Id
                    });
                    model.CreateTime = DateTime.Now;
                    model.Id         = loginLogId;
                }

                return(model);
            }
            return(null);
        }