Ejemplo n.º 1
0
Archivo: MsgKit.cs Proyecto: Daoting/dt
        /// <summary>
        /// 若用户在线则推送消息,不在线返回false
        /// </summary>
        /// <param name="p_userID"></param>
        /// <param name="p_msg"></param>
        /// <returns>true 已在线推送,false不在线</returns>
        public static async Task <bool> PushIfOnline(long p_userID, MsgInfo p_msg)
        {
            if (Online.All.TryGetValue(p_userID, out var ls) &&
                ls != null &&
                ls.Count > 0)
            {
                // 本地在线推送
                ls[0].AddMsg(p_msg.GetOnlineMsg());
                return(true);
            }

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

            if (cnt > 1)
            {
                string key = $"msg:PushIfOnline:{p_userID}:{Guid.NewGuid().ToString().Substring(0, 6)}";
                Kit.RemoteMulticast(new OnlinePushEvent
                {
                    PrefixKey = key,
                    Receivers = new List <long> {
                        p_userID
                    },
                    Msg = p_msg.GetOnlineMsg(),
                    PushFirstSession = true
                });

                // 等待收集
                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);
        }
Ejemplo n.º 2
0
Archivo: MsgKit.cs Proyecto: Daoting/dt
        /// <summary>
        /// 向所有副本的所有在线用户广播信息
        /// </summary>
        /// <param name="p_msg"></param>
        public static async Task BroadcastAllOnline(MsgInfo p_msg)
        {
            Throw.IfNull(p_msg);

            var msg = p_msg.GetOnlineMsg();
            int cnt = await Kit.GetSvcReplicaCount();

            if (cnt > 1)
            {
                // 多副本推送
                Kit.RemoteMulticast(new BroadcastEvent {
                    Msg = msg
                });
            }
            else
            {
                // 本地单副本推送
                foreach (var ls in Online.All.Values)
                {
                    foreach (var ci in ls)
                    {
                        ci.AddMsg(msg);
                    }
                }
            }
        }
Ejemplo n.º 3
0
Archivo: MsgKit.cs Proyecto: Daoting/dt
        /// <summary>
        /// 向用户列表中的在线用户推送信息
        /// </summary>
        /// <param name="p_userIDs">用户列表</param>
        /// <param name="p_msg">待推送信息</param>
        /// <returns>在线推送列表</returns>
        public static async Task <List <long> > Push(List <long> p_userIDs, MsgInfo p_msg)
        {
            if (p_userIDs == null || p_userIDs.Count == 0 || p_msg == null)
            {
                throw new Exception("待推送的用户或信息不可为空!");
            }

            var    onlines   = new List <long>();
            var    offlines  = new List <long>();
            string onlineMsg = p_msg.GetOnlineMsg();
            int    cnt       = await Kit.GetSvcReplicaCount();

            if (cnt > 1)
            {
                // Msg服务多副本推送
                await PushMultipleReplicas(p_userIDs, onlineMsg, onlines, offlines, cnt);
            }
            else
            {
                // 本地单副本推送
                PushLocal(p_userIDs, onlineMsg, onlines, offlines);
            }

            // 离线
            if (offlines.Count > 0)
            {
                // 将消息保存到用户的未推送列表
                var lc = new ListCache <string>(MsgQueueKey);
                foreach (long id in offlines)
                {
                    await lc.RightPush(id, onlineMsg);
                }

                // 推送离线提醒
                if (!string.IsNullOrEmpty(p_msg.Title))
                {
                    Offline.Add(offlines, p_msg);
                }
            }
            return(onlines);
        }