Ejemplo n.º 1
0
        public void UniCast(FoxundermoonLib.XmppEx.Data.Message message)
        {
            if (null == message.ToUser || string.IsNullOrEmpty(message.ToUser.Name))
            {
                Console.WriteLine("not set ToUser in the message");
            }
            if (string.IsNullOrEmpty(message.ToUser.Resource))
            {
                Console.WriteLine("not set Resource");
            }
            //throw new Exception("not set Resource");
            ConcurrentDictionary <string, XmppSeverConnection> cons = null;
            var hasCons = XmppConnectionDic.TryGetValue(message.ToUser.Name, out cons);

            if (!hasCons)
            {
                var tmp = Console.ForegroundColor;
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("the user are not online");
                Console.ForegroundColor = tmp;
            }
            else
            {
                XmppSeverConnection con = null;
                var hasCon = cons.TryGetValue(message.ToUser.Resource, out con);
                if (!hasCon)
                {
                    Console.WriteLine("the user of the resource are not onling");
                }
                else
                {
                    UniCast(con, message);
                }
            }
        }
Ejemplo n.º 2
0
 public void UserOffline(FoxundermoonLib.XmppEx.Data.User user)
 {
     try
     {
         ConcurrentDictionary <string, XmppSeverConnection> cons = null;
         var hasCons = XmppConnectionDic.TryGetValue(user.Name, out cons);
         if (hasCons)
         {
             XmppSeverConnection con = null;
             var hasCon = cons.TryGetValue(user.Resource, out con);
             if (hasCon)
             {
                 cons.TryRemove(user.Resource, out con);
             }
             var offLine = new FoxundermoonLib.XmppEx.Data.Message();
             offLine.Command.Name = FoxundermoonLib.XmppEx.Command.Cmd.UserOffLine;
             offLine.AddProperty("UserName", user.Name + "/" + user.Resource);
             Broadcast(offLine);
             if (UserOffLineHandler != null)
             {
                 UserOffLineHandler(user);
             }
         }
     }
     catch (Exception e)
     {
     }
 }
Ejemplo n.º 3
0
        private void ProcessIQAsync(agsXMPP.XmppSeverConnection contextConnection, IQ iq)
        {
            if (iq.Query.GetType() == typeof(Auth))
            {
                Auth   auth     = iq.Query as Auth;
                string name     = (auth.Username);
                string resource = auth.Resource;
                if (resource == null)
                {
                    resource = "";
                }
                var user = new FoxundermoonLib.XmppEx.Data.User(name, resource);
                switch (iq.Type)
                {
                case IqType.get:
                    iq.SwitchDirection();
                    iq.Type = IqType.result;
                    auth.AddChild(new Element("password"));
                    //auth.AddChild(new Element("digest"));
                    Console.WriteLine(auth.Username + " :开始登陆!");
                    contextConnection.Send(iq);
                    break;

                case IqType.set:
                    // Here we should verify the authentication credentials
                    Console.WriteLine(auth.Username + " : " + "开始验证, 密码:" + auth.Password);
                    iq.SwitchDirection();
                    if (AccountBus.CheckAccountAsync(auth.Username, auth.Password))      //验证用户是否存在或者密码是否正确
                    {
                        contextConnection.IsAuthentic = true;
                        iq.Type  = IqType.result;
                        iq.Query = null;
                        try
                        {
                            ConcurrentDictionary <string, XmppSeverConnection> cons = null;
                            //Func<int,XmppSeverConnection,XmppSeverConnection> update = (k,v)=>{return v;};
                            //XmppConnectionDic.AddOrUpdate(uid, contextConnection),(k,v)=>{return v;});
                            var hasCons = XmppConnectionDic.TryGetValue(name, out cons);
                            if (hasCons)
                            {
                                XmppSeverConnection con = null;
                                var hasCon = cons.TryGetValue(resource, out con);
                                if (hasCon)
                                {
                                    cons.TryRemove(resource, out con);
                                    Console.WriteLine(name + " 重新登录");
                                    try
                                    {
                                        //con.Stop();
                                    }
                                    catch (Exception e)
                                    {
                                        Console.WriteLine("[email protected] old connection  :" + e.Message);
                                    }
                                }
                            }

                            if (!hasCons)
                            {
                                cons = new ConcurrentDictionary <string, XmppSeverConnection>();
                                if (XmppConnectionDic.TryAdd(name, cons))
                                {
                                    Console.WriteLine(auth.Username + ": 账号验证成功,并加入连接池!");
                                }
                                else
                                {
                                    Console.WriteLine(auth.Username + ": 账号验证成功,但是加入连接池失败!");
                                }
                            }

                            cons.TryAdd(resource, contextConnection);
                            contextConnection.User = user;
                            UserOnline(user);
                        }
                        catch (Exception e)
                        {
                            // 消息没有 From    dosomething
                            iq.Type  = IqType.error;
                            iq.Value = e.Message;
                            Console.WriteLine("Exception --> message: " + e.Message + "  data:" + e.Data);
                        }
                    }
                    else
                    {
                        // authorize failed
                        iq.Type = IqType.error;      //若要开启验证功能去掉此注释
                        Console.WriteLine(auth.Username + ":账号验证失败!");
                        FoxundermoonLib.XmppEx.Data.Message loginFailed = new FoxundermoonLib.XmppEx.Data.Message();
                        loginFailed.Command.Name = FoxundermoonLib.XmppEx.Command.Cmd.ErrorMessage;
                        loginFailed.AddProperty("Cause", "账号验证失败,请检查用户名或者密码");
                        loginFailed.ToUser = user;
                        UniCast(loginFailed);
                        //iq.Type = IqType.result;
                        iq.Query = null;
                        iq.Value = "authorized failed";
                        contextConnection.IsAuthentic = false;
                    }
                    try
                    {
                        contextConnection.Send(iq);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Exception->@IQhandler.processIq:" + e.Message);
                    }
                    break;
                }
            }
            else if (!contextConnection.IsAuthentic)
            {
                contextConnection.Stop();
            }
            else if (iq.Query.GetType() == typeof(Roster))
            {
                ProcessRosterIQ(contextConnection, iq);
            }
        }