XMPP(2)agsXMPP实现细节浅析

php中文网
发布: 2016-06-07 15:26:35
原创
1504人浏览过

using System; using System.IO; using System.Text; using System.Threading; using System.Net; using System.Net.Sockets; using agsXMPP.protocol; using agsXMPP.protocol.iq; using agsXMPP.protocol.iq.auth; using agsXMPP.protocol.iq.roster; usin

using system;

using System.IO;

using System.Text;

using System.Threading;

using System.Net;

using System.Net.Sockets;

 

using agsXMPP.protocol;

using agsXMPP.protocol.iq;

using agsXMPP.protocol.iq.auth;

using agsXMPP.protocol.iq.roster;

using agsXMPP.protocol.client;

 

using agsXMPP.Xml;

using agsXMPP.Xml.Dom;

 

namespace agsXMPP

{

     ///

     /// Zusammenfassung f黵XMPPSeverConnection.

     ///

     public class 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);  

           

         }

JoinMC智能客服
JoinMC智能客服

JoinMC智能客服,帮您熬夜加班,7X24小时全天候智能回复用户消息,自动维护媒体主页,全平台渠道集成管理,电商物流平台一键绑定,让您出海轻松无忧!

JoinMC智能客服 193
查看详情 JoinMC智能客服

 

         public XmppSeverConnection(Socket sock) : this()

         {   

              m_Sock = sock;

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

         }

         #endregion

        private StreamParser              streamParser;

         private Socket                       m_Sock;

        private const int BUFFERSIZE = 1024;

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

               

    

         public void ReadCallback(IAsyncResult ar)

         {       

              // Retrieve the state object and the handler socket

              // from the asynchronous state object

 

              // Read data from the client socket.

              int bytesRead = m_Sock.EndReceive(ar);

 

              if (bytesRead > 0)

              {                 

                   streamParser.Push(buffer, 0, bytesRead);

                  

                   // Not all data received. Get more.

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

              }

              else

              {

                   m_Sock.Shutdown(SocketShutdown.Both);

                   m_Sock.Close();

              }

         }

 

         private void Send(string data)

         {

              // Convert the string data to byte data using ASCII encoding.

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

 

              // Begin sending the data to the remote device.

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

         }

 

         private void SendCallback(IAsyncResult ar)

         {

              try

              {

                   // Complete sending the data to the remote device.

                   int bytesSent = m_Sock.EndSend(ar);

                   Console.WriteLine("Sent {0} bytes to client.", bytesSent);

 

              }

              catch (Exception e)

              {

                   Console.WriteLine(e.ToString());

              }

         }

    

        

         public void Stop()

         {

              Send("");

//            client.Close();

//            _TcpServer.Stop();

 

              m_Sock.Shutdown(SocketShutdown.Both);

              m_Sock.Close();

         }

             

        

         #region >

//       private int            m_Port             = 5222;      

         private string         m_SessionId        = null;

 

         public string SessionId

         {

              get

              {

                   return m_SessionId;

              }

              set

              {

                   m_SessionId = value;

              }

         }

         #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))

              {

                   // route presences here and handle all subscription stuff

              }

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

              {

                   // route the messages here

 

              }

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

              {

                   ProcessIQ(e as 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("password"));

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

                            Send(iq);

                            break;

                       case IqType.set:

                            // Here we should verify the authentication credentials

                            iq.SwitchDirection();

                            iq.Type = IqType.result;

                            iq.Query = null;

                            Send(iq);

                            break;

                   }

                  

              }

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

              {

                   ProcessRosterIQ(iq);

                  

              }

             

         }

 

         private void ProcessRosterIQ(IQ iq)

         {

              if (iq.Type == IqType.get)

              {

                   // Send the roster

                   // we send a dummy roster here, you should retrieve it from a

                   // database or some kind of directory (LDAP, AD etc...)

                   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);

                   }

                   Send(iq);

              }

         }

 

         private void SendOpenStream()

         {

             

              // Recv:

             

              // Send the Opening Strem to the client

              string ServerDomain = "localhost";

             

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

             

             

              StringBuilder sb = new StringBuilder();

 

              sb.Append( "" );

 

              Send( sb.ToString() );

         }

 

         private void Send(Element el)

         {

              Send(el.ToString());

         }

 

     }

}

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号