Beispiel #1
0
        /// <summary>
        /// 添加指定用户的连接
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="connectionId">连接Id</param>
        /// <returns></returns>
        public virtual async Task AddConnectionId(string userName, string connectionId)
        {
            ConnectionUser user = await GetUser(userName) ?? new ConnectionUser()
            {
                UserName = userName
            };

            user.ConnectionIds.AddIfNotExist(connectionId);
            await SetUser(userName, user);
        }
Beispiel #2
0
        /// <summary>
        /// 移除指定用户的连接
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="connectionId">连接Id</param>
        /// <returns></returns>
        public virtual async Task RemoveConnectionId(string userName, string connectionId)
        {
            ConnectionUser user = await GetUser(userName);

            if (user == null || !user.ConnectionIds.Contains(connectionId))
            {
                return;
            }
            user.ConnectionIds.Remove(connectionId);
            if (user.ConnectionIds.Count == 0)
            {
                await RemoveUser(userName);

                return;
            }
            await SetUser(userName, user);
        }
Beispiel #3
0
 /// <summary>
 /// 设置用户缓存
 /// </summary>
 /// <param name="userName">用户名</param>
 /// <param name="user">用户信息</param>
 /// <returns></returns>
 public virtual async Task SetUser(string userName, ConnectionUser user)
 {
     string key = GetKey(userName);
     await _cache.SetAsync(key, user);
 }
Beispiel #4
0
        /// <summary>
        /// 获取指定用户的所有连接Id
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <returns></returns>
        public virtual async Task <string[]> GetConnectionIds(string userName)
        {
            ConnectionUser user = await GetUser(userName);

            return(user?.ConnectionIds.ToArray() ?? new string[0]);
        }