示例#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);
                }
            }
        }
示例#2
0
        public void UniCast(XmppSeverConnection contexCon, FoxundermoonLib.XmppEx.Data.Message message)
        {
            Message msg = new Message();

            if (null == message.FromUser)
            {
                msg.From = ServerJid;
            }
            else
            {
                msg.From = getJidFromUser(message.FromUser);
            }
            if (null != message.ToUser)
            {
                msg.To = getJidFromUser(message.ToUser);
            }
            msg.Language = "BASE64";
            msg.Subject  = message.GetJsonCommand();
            msg.Body     = FoxundermoonLib.Encrypt.EncryptUtil.EncryptBASE64ByGzip(message.ToJson());
            try
            {
                contexCon.Send(msg);
            }
            catch (Exception e)
            {
                Console.WriteLine("*****@*****.**" + e.Message);
            }
        }
示例#3
0
        public void Broadcast2resource(FoxundermoonLib.XmppEx.Data.Message message, string resource)
        {
            Message msg = new Message();

            if (null == message.FromUser)
            {
                msg.From = getJidFromUser(message.FromUser);
            }
            else
            {
                msg.From = getJidFromUser(message.FromUser);
            }
            msg.Body     = FoxundermoonLib.Encrypt.EncryptUtil.EncryptBASE64ByGzip(message.ToJson());
            msg.Subject  = message.GetJsonCommand();
            msg.Language = "BASE64";

            foreach (var cons in XmppConnectionDic)
            {
                XmppSeverConnection con = null;
                var hasCon = cons.Value.TryGetValue(resource, out con);
                if (hasCon)
                {
                    Jid to = new Jid(cons.Key + "@" + Config.ServerIp + "/" + resource);
                    msg.To = to;
                    try
                    {
                        con.Send(msg);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("[email protected] message:" + e.Message);
                    }
                }
            }
        }
示例#4
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)
     {
     }
 }
示例#5
0
 private static void AcceptClientConnect()
 {
     while (true)
     {
         Socket proxSocket       = listenSocket.Accept();
         XmppSeverConnection con = new XmppSeverConnection(proxSocket);
     }
 }
示例#6
0
        public void AcceptCallback(IAsyncResult ar)
        {
            // Signal the main thread to continue.
            allDone.Set();
            // Get the socket that handles the client request.
            var sock = m_Listener.EndAccept(ar);

            var con = new XmppSeverConnection(sock);
        }
示例#7
0
 //处理客户端与服务器的通信   在此处加入服务器相关业务逻辑代码
 private void HandleServerMsg(XmppSeverConnection con, Message msg)
 {
     if (msg.Body == "123456")
     {
         Message m = new Message();
         m.Body = "-----------------------";
         m.From = new Jid("1@localhost");
         m.To   = msg.From;
         con.Send(m);
     }
 }
 public void OnMessage(XmppSeverConnection contextConnection, Message message)
 {
     if (contextConnection.IsAuthentic)
     {
         processMessage(contextConnection, message);
     }
     else
     {
         contextConnection.Stop();
     }
 }
 public void OnNode(XmppSeverConnection contextConnection, Node node)
 {
     //process all the node;
     //ig nore
     //if(node.GetType() == typeof(Element)) {
     //    Element e = node as Element;
     //    if(e.HasTag("base64")) {
     //        e.Value = EncryptUtil.DecryptBASE64ByGzip(e.Value);
     //    }
     //}
 }
示例#10
0
        public void Handle(XmppSeverConnection con, Message msg)
        {
            if (true)
            {
                HandleServerMsg(con, msg);
            }
            else
            {
                try
                {
                    int fromUid = int.Parse(msg.From.User);

                    int toUid = int.Parse(msg.To.User);
                    if (!_conDic.ContainsKey(fromUid))
                    {
                        _conDic.Add(fromUid, con);                      //以发送者的uid作为key存储发送者与服务器之间的连接
                    }
                    Models.Message msgModel = mentiti.Message.Create(); //数据库实体
                    msgModel.From = int.Parse(msg.From.User);
                    msgModel.To   = int.Parse(msg.To.User);
                    XmppSeverConnection tmpCon;  //根据uid  选择对应的 connection 转发数据
                    if (_conDic.TryGetValue(toUid, out tmpCon))
                    {
                        tmpCon.Send(msg);
                        msgModel.Status = 1; //已发送
                    }
                    else
                    {
                        msgModel.Status = 0; //未发送
                    }
                    mentiti.Message.Add(msgModel);

                    try
                    {
                        //保存入库
                        mentiti.SaveChanges();
                    }
                    catch (Exception e)
                    {
                    }
                }
                catch (Exception ae)
                {
                }
            }
        }
示例#11
0
        private void AcceptCallback(IAsyncResult ar)
        {
            // Signal the main thread to continue.
            allDone.Set();
            // Get the socket that handles the client request.
            Socket newSock = listener.EndAccept(ar);

            Console.WriteLine("从 " + newSock.RemoteEndPoint.ToString() + "建立了一条tcp连接");

            XmppSeverConnection con = new XmppSeverConnection(newSock, this);

            con.OnNode     += new NodeHandler(OnNode);
            con.OnIq       += new IqHandler(OnIQ);
            con.OnMessage  += new MessageHandler(OnMessage);
            con.OnPresence += new PresenceHandler(OnPresence);
            /// you can  register other handler  here
            //listener.BeginReceive(buffer, 0, BUFFERSIZE, 0, new AsyncCallback(ReadCallback), null);
        }
示例#12
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);
            }
        }
示例#13
0
 public async void OnIQ(XmppSeverConnection contextConnection, IQ iq)
 {
     ProcessIQAsync(contextConnection, iq);
 }