コード例 #1
0
ファイル: Pusher.cs プロジェクト: Daoting/dt
        /// <summary>
        /// 实时获取所有副本的在线用户总数
        /// </summary>
        /// <returns>Dict结构:key为副本id,value为副本会话总数</returns>
        public async Task <Dict> GetOnlineCount()
        {
            Dict result = null;
            int  cnt    = await Kit.GetSvcReplicaCount();

            if (cnt > 1)
            {
                // 所有副本
                string key = "msg:OnlineCount:" + Guid.NewGuid().ToString().Substring(0, 6);
                Kit.RemoteMulticast(new OnlineCountEvent {
                    CacheKey = key
                });

                // 等待收集
                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);

                    result = hash.ToDict();
                }
            }
            else
            {
                // 当前单副本
                result = new Dict {
                    { Kit.SvcID, Online.TotalCount }
                };
            }
            return(result);
        }
コード例 #2
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);
        }