Wednesday, January 25, 2012

Working with XMPP in .Net using agsXMPP SDK

XMPP

The Extensible Messaging and Presence Protocol (XMPP) is an open technology for real-time communication, which powers a wide range of applications including instant messaging, presence, multi-party chat, voice and video calls, collaboration, lightweight middleware, content syndication, and generalized routing of XML data.

How to work with XMPP in .Net?

agsXMPP is a SDK / library for the eXtensibleMessaging and Presence Protocol (XMPP) protocol written in managed C# dedicated to .NET and Mono technologies. The SDK is released as open source.

SDK could be used for XMPP client, server and component development.

SDK Download Path: http://www.ag-software.de/download-directory/

Here is a small sample how easy you can login to a XMPP Server.

a) I tried Connecting to my Gtalk Account through Jabber Server and tried Changing the Status Message Dynamically(Eg:Showing a CountDown for NewYear).

b) Sending auto reply to contacts in Gtalk while away.

3) Showing Current cricket live Score in your status message.

objXmpp = new agsXMPP.XmppClientConnection();

agsXMPP.Jid jid = null;

jid = new agsXMPP.Jid("my gmail id");

objXmpp.Password = "my gmail password";

objXmpp.Username = jid.User;

objXmpp.Server = jid.Server;

objXmpp.AutoResolveConnectServer = true;

objXmpp.Status = "Available J";

try

{

objXmpp.OnMessage += messageReceived;

objXmpp.OnAuthError += loginFailed;

objXmpp.OnLogin += loggedIn;

objXmpp.Open();

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

private void loggedIn(object sender)//On Successful Login to your Account

{

MessageBox.Show("Logged In");

MessageBox.Show(objXmpp.Authenticated.ToString());

}

private void loginFailed(object o, agsXMPP.Xml.Dom.Element el)//On Login Failure

{

MessageBox.Show("Loggin Failed");

}

private void messageReceived(object sender, agsXMPP.protocol.client.Messagemsg)//Handling Auto Reply on Message received

{

string[] chatMessage = null;

chatMessage = msg.From.ToString().Split('/');

agsXMPP.Jid jid = null;

jid = new agsXMPP.Jid(chatMessage[0]);

agsXMPP.protocol.client.Message autoReply = null;

autoReply = new agsXMPP.protocol.client.Message(jid, agsXMPP.protocol.client.MessageType.chat, "This is Auto reply");

objXmpp.Send(autoReply);

}

private void StatusTimer_Tick(object sender, EventArgs e)//To Show a Countdown

{

TimeSpan span = TargetDate.Subtract (System .DateTime ..Now );

string status = "Days More for My Bday:DD" + span.Days.ToString() +"hh" + span.Hours.ToString() + "mm:" + span.Minutes.ToString() + "ss:" + span.Seconds ..ToString();

Presence p = new Presence(ShowType.chat, status);

p.Type = PresenceType.available;

objXmpp.Send(p);

}

//To Get Live Cricket Score from a RSS Feed

private void GetLiveCricketScore()

{

try

{

// create here table and columns that will be used

// for storing items from the xml file for later binding into grid

DataTable dtable = new DataTable();

dtable.Columns.Add(new DataColumn("SlNo"));

dtable.Columns.Add(new DataColumn("title"));

dtable.Columns.Add(new DataColumn("link"));

// fetch webrequest. Here, give the path of the location where rss feed is stored.

//WebRequest WebReq = WebRequest.Create("http://static.cricinfo.com/rss/livescores.xml");

WebRequest WebReq =WebRequest.Create(" http://static.cricinfo.com/rss/livescores.xml ");

// get webresponse from the webrequset

WebResponse webRes = WebReq.GetResponse();

// use stream to stremline the input from from webresponse.

Stream rssStream = webRes.GetResponseStream();

// Create new xml document and load a XML Document

// with the strem.

XmlDocument xmlDoc = new XmlDocument();

// loads the url from the stream

xmlDoc.Load(rssStream);

// use XmlNodeList to get the matching xmlnodes from the xmldocument

XmlNodeList xmlNodeList = xmlDoc.SelectNodes("rss/channel/item");

// create array of the object for creating the row

object[] RowValues = {"", "", "" };

// Make a Loop through RSS Feed items

for (int i = 0; i < xmlNodeList.Count; i++)

{

XmlNode xmlNode;

RowValues[0] = i;

xmlNode = xmlNodeList.Item(i).SelectSingleNode("title");

if (xmlNode != null)

{

RowValues[1] = RemoveDigitsAndSpecialCharacters(xmlNode.InnerText);

}

else

{

RowValues[1] = "";

}

xmlNode = xmlNodeList.Item(i).SelectSingleNode("link");

if (xmlNode != null)

{

RowValues[2] = RemoveDigitsAndSpecialCharacters(xmlNode.InnerText);

}

else

{

RowValues[2] = "";

}

// creating datarow and add it to the datatable

DataRow dRow;

dRow = dtable.Rows.Add(RowValues);

dtable.AcceptChanges();

}

cmbCurrentMatches.DataSource = dtable;

cmbCurrentMatches.DisplayMember = "title";

}

catch (WebException)

{

MessageBox.Show("Not Able to Access Cricket Live Feeds");

}

catch (XmlException)

{

MessageBox.Show("The received Feed was not in a proper format");

}

}

No comments:

Post a Comment

Post a Comment

 
Twitter Bird Gadget