XMPP(2)agsXMPP实现细节浅析

WBOY
发布: 2016-06-07 15:26:35
原创
1300 人浏览过

使用系统;使用系统.IO;使用系统文本;使用系统线程;使用System.Net;使用 System.Net.Sockets;使用 agsXMPP. 协议;使用 agsXMPP.protocol.iq;使用 agsXMPP.protocol.iq.auth;使用 agsXMPP.protocol.iq.roster;使用

使用系统;

使用 System.IO;

使用 System.Text;

使用 System.Threading;

使用 System.Net;

使用 System.Net.Sockets;

 

使用 agsXMPP.protocol;

使用 agsXMPP.protocol.iq;

使用 agsXMPP.protocol.iq.auth;

使用 agsXMPP.protocol.iq.roster;

使用 agsXMPP.protocol.client;

 

使用 agsXMPP.Xml;

使用 agsXMPP.Xml.Dom;

 

命名空间 agsXMPP

{

     ///

     /// Zusammenfassung f黵XMPPSeverConnection。

     /// 摘要>

     公共类 XmppSeverConnection

     {

         #region >

         public XmppSeverConnection()

         {       

             streamParser = new StreamParser();

             streamParser.OnStreamStart       += new StreamHandler(streamParser_OnStreamStart);

             streamParser.OnStreamEnd         += new StreamHandler(streamParser_OnStreamEnd);

             streamParser.OnStreamElement     += new StreamHandler(streamParser_OnStreamElement);  

           

         }

 

         public XmppSeverConnection(Socket sock) : this()

         {   

              m_Sock = 袜子;

              m_Sock.BeginReceive(buffer, 0, BUFFERSIZE, 0, new AsyncCallback(ReadCallback), null);       

         }

         #endregion

        private StreamParser              streamParser;

         私有套接字                       m_Sock;

        private const int BUFFERSIZE = 1024;

        private byte[] buffer = new byte[BUFFERSIZE];

               

    

         public void ReadCallback(IAsyncResult ar)

         {       

              // 检索状态对象和处理程序套接字

              // 来自异步状态对象

 

              // 从客户端套接字读取数据。

              int bytesRead = m_Sock.EndReceive(ar);

 

              if (bytesRead > 0)

              {                 

                  streamParser.Push(buffer, 0, bytesRead);

                  

                   // 未收到所有数据。获取更多。

                   m_Sock.BeginReceive(buffer, 0, BUFFERSIZE, 0, new AsyncCallback(ReadCallback), null);

              }

              其他

              {

                   m_Sock.Shutdown(SocketShutdown.Both);

                   m_Sock.Close();

              }

         }

 

         private void Send(字符串数据)

         {

              // 使用 ASCII 编码将字符串数据转换为字节数据。

              byte[] byteData = Encoding.UTF8.GetBytes(data);

 

              // 开始将数据发送到远程设备。

              m_Sock.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), null);

         }

 

         private void SendCallback(IAsyncResult ar)

         {

              尝试

              {

                   // 完成将数据发送到远程设备。

                   int bytesSent = m_Sock.EndSend(ar);

                   Console.WriteLine("已向客户端发送 {0} 个字节。", bytesSent);

 

              }

              catch(例外 e)

              {

                   Console.WriteLine(e.ToString());

              }

         }

    

        

         public void Stop()

         {

              发送("");

//            client.Close();

//            _TcpServer.Stop();

 

              m_Sock.Shutdown(SocketShutdown.Both);

              m_Sock.Close();

         }

             

        

         #region >

//       private int            m_Port             = 5222;      

         私有字符串         m_SessionId        = null;

 

         公共字符串 SessionId

         {

              获取

              {

                   return m_SessionId;

              }

              套装

              {

                   m_SessionId = 值;

              }

         }

         #endregion

 

         private void streamParser_OnStreamStart(object sender, Node e)

         {

              SendOpenStream();

         }

 

         private void streamParser_OnStreamEnd(object sender, Node e)

         {

 

         }

 

         private void streamParser_OnStreamElement(object sender, Node e)

         {

            Console.WriteLine("OnStreamElement: " + e.ToString());

              if (e.GetType() == typeof(Presence))

              {

                   // 在此处路由存在并处理所有订阅内容

              }

              else if (e.GetType() == typeof(Message))

              {

                   // 在此处路由消息

 

              }

              else if (e.GetType() == typeof(IQ))

              {

                   ProcessIQ(e 为 IQ);

              }

         }

 

         private void ProcessIQ(IQ iq)

         {

              if(iq.Query.GetType() == typeof(Auth))

              {

                   Auth auth = iq.Query as Auth;

                   switch(iq.Type)

                   {

                       case IqType.get:

                            iq.SwitchDirection();

                            iq.Type = IqType.result;

                            auth.AddChild(new Element("密码"));

                            auth.AddChild(new Element("digest"));

                            发送(iq);

                            休息;

                       case IqType.set:

                            // 这里我们应该验证身份验证凭据

                            iq.SwitchDirection();

                            iq.Type = IqType.result;

                            iq.Query = null;

                            发送(iq);

                            休息;

                   }

                  

              }

              else if(iq.Query.GetType() == typeof(Roster))

              {

                   ProcessRosterIQ(iq);

                  

              }

             

         }

 

         private void ProcessRosterIQ(IQ iq)

         {

              if (iq.Type == IqType.get)

              {

                   // 发送名单

                   // 我们在此处发送了一个虚拟名单,您应该从

检索它

                   // 数据库或某种目录(LDAP、AD 等...)

                   iq.SwitchDirection();

                   iq.Type = IqType.result;

                   for (int i=1; i

                   {

                       RosterItem ri = new RosterItem();

                       ri.Name = "Item " + i.ToString();

                       ri.Subscription = SubscriptionType.both;

                       ri.Jid = new Jid("item" + i.ToString() + "@localhost");

                       ri.AddGroup("localhost");

                       iq.Query.AddChild(ri);

                   }

                   for (int i=1; i

                   {

                       RosterItem ri = new RosterItem();

                       ri.Name = "Item JO " + i.ToString();

                       ri.Subscription = SubscriptionType.both;

                       ri.Jid = new Jid("item" + i.ToString() + "@jabber.org");

                       ri.AddGroup("JO");

                       iq.Query.AddChild(ri);

                   }

                   发送(iq);

              }

         }

 

         private void SendOpenStream()

         {

             

              // 接收:

             

              // 将开头字符串发送给客户端

              string ServerDomain = "localhost";

             

              this.SessionId = agsXMPP.SessionId.CreateNewId();

             

             

              StringBuilder sb = new StringBuilder();

 

              sb.Append( "" );

 

              发送( sb.ToString() );

         }

 

         private void Send(Element el)

         {

              发送(el.ToString());

         }

 

     }

}

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板