Friday 28 November 2014

Using XMPP and Openfire server in ASP.NET C# - Part -2

Hi check out my new blog post here on retrieving roster from openfire server in asp.net.

Roster in XMPP


Roster are the contacts of  user (contact list).



Roster Handlers in AgsXmpp


Let us discuss, the handlers other than the mentioned in part -1 i.e., (loggedin, loginfailed) are mentioned below

·         OnRosterItem

·         OnRosterEnd

·         OnRosterStart



Let us recap part -1 (http://www.ecanarys.com/blog-entry/using-xmpp-and-openfire-server-aspnet-c-part-1 ) login part (we need here that code snippet for initializing the above mentioned handlers)



AgsXMPP.XmppClientConnection objXmpp = new agsXMPP.XmppClientConnection ();

Jid jid = new Jid ("xyz@server name"); //ex: xyz@abc.com –abc is server name

objXmpp.Server = jid.Server;

objXmpp.Username = jid.User;

objXmpp.Password = ******; //your password of account.

objXmpp.AutoResolveConnectServer = true;

Try

{

 objXmpp.OnLogin += loggedIn; // loggedIn is handler for successful login to server.

 objXmpp.OnAuthError += loginFailed;

objXmpp. OnRosterStart+= new ObjectHandler(objXmpp_OnRosterStart);

objXmpp.OnRosterItem+=new AgsXMPP.XmppClientConnection.RosterHandler(objXmpp_OnRosterItem);

objXmpp. OnRosterEnd+= new ObjectHandler(objXmpp _OnRosterEnd);

objXmpp.Open ();

}

Catch (exception ex)

{ }



OnRosterStart handler is shown below

 private  void objXmpp_OnRosterStart(object sender)

 {

 }

Onrosterstart handler is executed after the login is successful,  implies initiate any method or variable (ex. DataTable or Generic list to store the contacts ) to handle the Roster List item which is next event.



The OnRosterItem handler is shown below

private void objXmpp_OnRosterItem(object sender, RosterItem rosterItem)

{

    //rosterItem.Jid

    // rosterItem.Subscription

}

In the above mentioned handler, we can read every roster information (contact list) after successful OnRosterStart .



We can get below mentioned properties from RosterItem

·         Subscription (SubscriptionType enum )

·         JId



SubscriptionType is an enum with values

·         None – (not yet subscribed)

·         To – (sender subscribed waiting for your approval)

·         From – (receiver not accepted your subscription)

·         Both – (Both receiver and sender accepted the request)

In this event, you can bind the contacts to the required type as explained in onrosterstart.



Add New Roster
We can add new roster to the contacts list by using RosterManager in OnRoserItem

private void addNewRoster(Jid contactJid)

{

    objXmpp.RosterManager.AddRosterItem(contactJid); // Add new

    objXmpp.RosterManager.UpdateRosterItem(contactJid, "nickname"); //update

    objXmpp.RosterManager.RemoveRosterItem(contactJid); // remove

 }

Approve Subscription
We can approve subscription according to the above mentioned subscription type

private void subcribeRoster(Jid contactJid)

 {

      objXmpp.PresenceManager.ApproveSubscriptionRequest(contactJid);

 }

OnRosterEnd handler is shown below

private void objXmpp_OnRosterEnd(object sender)
 {

 }

This handler assures that all Roster all delivered.

(Note: - We cannot use any asp.net controls in these handlers to display or update values. Since, these handlers are asynchronous.)

In Part 1 we discussed about Login and Login failed handler.

In Part 2 we discussed about Roster Handlers

And , In Part 3 we shall discuss about sending and Receiving Messages using AgsXmpp.


Firsted posted on 
http://www.ecanarys.com/blog-entry/using-xmpp-and-openfire-server-aspnet-c-part-2

Please, do post feedback.

No comments:

Post a Comment