示例#1
0
        public async Task Handle(UserSessionsEvent p_event)
        {
            var ls = Online.GetSessions(p_event.UserID);

            if (ls != null && ls.Count > 0)
            {
                var result = new List <Dict>();
                foreach (var ci in ls)
                {
                    result.Add(new Dict
                    {
                        { "userid", ci.UserID },
                        { "svcid", Kit.SvcID },
                        { "starttime", ci.StartTime.ToString() },
                        { "platform", ci.Platform },
                        { "version", ci.Version },
                        { "devicename", ci.DeviceName },
                        { "devicemodel", ci.DeviceModel },
                    });
                }
                var msg = Kit.Serialize(result);
                await Kit.HashSetField(p_event.CacheKey, null, p_event.UserID.ToString(), msg);
            }

            // 统计总数
            await Kit.StringIncrement(p_event.CacheKey, "cnt");
        }
示例#2
0
        public async Task Handle(IsOnlineEvent p_event)
        {
            var sc = new StringCache(p_event.CacheKey);
            var ls = Online.GetSessions(p_event.UserID);

            if (ls != null && ls.Count > 0)
            {
                await sc.Set(null, "true");
            }
            // 统计总数+1
            await sc.Increment("cnt");
        }
示例#3
0
文件: Pusher.cs 项目: Daoting/dt
        /// <summary>
        /// 判断用户是否在线,查询所有副本
        /// </summary>
        /// <param name="p_userID"></param>
        /// <returns>false 不在线</returns>
        public async Task <bool> IsOnline(long p_userID)
        {
            var ls = Online.GetSessions(p_userID);

            if (ls != null && ls.Count > 0)
            {
                return(true);
            }

            // 查询所有其他副本
            int cnt = await Kit.GetSvcReplicaCount();

            if (cnt > 1)
            {
                string key = $"msg:IsOnline:{p_userID}:{Guid.NewGuid().ToString().Substring(0, 6)}";
                Kit.RemoteMulticast(new IsOnlineEvent {
                    CacheKey = key, UserID = p_userID
                });

                // 等待收集
                int total, retry = 0;
                var sc = new StringCache(key);
                do
                {
                    await Task.Delay(_delayMilli);

                    total = await sc.Get <int>("cnt");

                    retry++;
                }while (total < cnt && retry < _maxRetry);

                // 删除统计总数
                await sc.Delete("cnt");

                // 存在键值表示在线
                if (await sc.Delete(null))
                {
                    return(true);
                }
            }
            return(false);
        }
示例#4
0
文件: Pusher.cs 项目: Daoting/dt
        /// <summary>
        /// 查询所有副本,获取某账号的所有会话信息
        /// </summary>
        /// <param name="p_userID"></param>
        /// <returns>会话信息列表</returns>
        public async Task <List <Dict> > GetAllSessions(long p_userID)
        {
            List <Dict> result = new List <Dict>();
            int         cnt    = await Kit.GetSvcReplicaCount();

            if (cnt > 1)
            {
                // 查询所有副本
                string key = $"msg:Sessions:{p_userID}:{Guid.NewGuid().ToString().Substring(0, 6)}";
                Kit.RemoteMulticast(new UserSessionsEvent {
                    CacheKey = key, UserID = p_userID
                });

                // 等待收集
                int total, retry = 0;
                var sc = new StringCache(key);
                do
                {
                    await Task.Delay(_delayMilli);

                    total = await sc.Get <int>("cnt");

                    retry++;
                }while (total < cnt && retry < _maxRetry);

                // 删除统计总数
                await sc.Delete("cnt");

                var hc   = new HashCache(key);
                var hash = await hc.GetAll(null);

                if (hash != null && hash.Length > 0)
                {
                    await hc.Delete(null);

                    var dt = hash.ToDict();
                    foreach (var item in dt)
                    {
                        var ss = Kit.Deserialize <List <Dict> >((string)item.Value);
                        if (ss != null && ss.Count > 0)
                        {
                            result.AddRange(ss);
                        }
                    }
                }
            }
            else
            {
                // 当前单副本
                var ls = Online.GetSessions(p_userID);
                if (ls != null && ls.Count > 0)
                {
                    foreach (var ci in ls)
                    {
                        result.Add(new Dict
                        {
                            { "userid", ci.UserID },
                            { "svcid", Kit.SvcID },
                            { "starttime", ci.StartTime.ToString() },
                            { "platform", ci.Platform },
                            { "version", ci.Version },
                            { "devicename", ci.DeviceName },
                            { "devicemodel", ci.DeviceModel },
                        });
                    }
                }
            }
            return(result);
        }