agsxmpp c mysql_XMPP(2)agsXMPP实现细节浅析

本文详细介绍了使用agsXMPP库在C#中建立XMPP服务器连接的过程,包括接收和发送数据、处理XML流、验证认证以及处理 roster IQ 请求。示例代码展示了如何响应客户端的stream开始、结束以及处理各种协议元素。
摘要由CSDN通过智能技术生成

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 << Constructors >>

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 = 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 << Properties and Member Variables >>

// 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<11;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<11;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());

}

}

}

f68f2add0b68e4f9810432fce46917b7.png

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值