Ejemplo n.º 1
0
        public int UnsubscribeCodes(string username, string[] codelist)
        {
            if (_dataAdapter != null)
            {
                List <string> codes = new List <string>();
                for (int i = 0; i < codelist.Length; i++)
                {
                    string code = codelist[i];
                    if (_codes.ContainsKey(code))
                    {
                        SubCode sc = _codes[code];
                        if (sc.Users.Contains(username))
                        {
                            sc.Users.Remove(username);
                            if (sc.Users.Count == 0)
                            {
                                //取消订阅代码
                                _codes.Remove(code);
                                codes.Add(code);
                            }
                        }

                        if (username == "")
                        {
                            //取消订阅代码
                            _codes.Remove(code);
                            codes.Add(code);
                        }
                    }
                }
                if (_clients.ContainsKey(username))
                {
                    //客户端取消订阅
                    _clients[username].RemoveSubCodes(codelist.ToList <string>());
                    if (_clients[username].SubCount == 0)
                    {
                        _clients.Remove(username);
                    }
                }

                if (username == "")
                {
                    //定时器取消长时间不使用的代码时清除用户引用
                    List <string> deluc = new List <string>();
                    foreach (UserClient uc in _clients.Values)
                    {
                        uc.RemoveSubCodes(codelist.ToList <string>());
                        if (uc.SubCount == 0)
                        {
                            deluc.Add(uc.Username);
                        }
                    }

                    foreach (string du in deluc)
                    {
                        if (_clients.ContainsKey(du))
                        {
                            _clients.Remove(du);
                        }
                    }
                }

                if (codes.Count > 0)
                {
                    _dataAdapter.DeRegisterCodes(codes);
                    App.Logger.Info(string.Format("DataService:{0}取消订阅,{1}", username, codes.ToString()));
                }
                return(codes.Count);
            }

            return(0);
        }