Saturday, July 2, 2011

XMPP or (How to programatically do stuffs in IM)

Hi.
It's been a while since I worked with the Smack API.... 6 months and so I might have forgotten many things. Anyways, it is a brief introduction to XMPP and more specifically to Smack API. It provides Java libraries for playing around with XMPP.



The above snippet will connect to the gmail server and make an status update.

Presence presence =new Presence(Presence.Type.available,
status,
24,
Presence.Mode.available);
Here Presence.Type is a enum to represent the presecence type. Note that presence type is 
often confused with presence mode. Generally, if a user is signed into a server, they have a 
presence type of available, even if the mode is away, dnd, etc. The presence type is only 
unavailable when the user is signing out of the server
status contains the string which you want to set as your status.
24 is the priority. The highest priory client will receive the packets.
Check this out for better documentation of Presence.


Add
Roster roster = connection.getRoster();
System.out.println("No of contacts: " + roster.getEntryCount());
for (RosterEntry entry : roster.getEntries())     
System.out.println("User: " + entry.getUser());
The above code will show the size of your roster and also show the user list

Message msg=new Message(user@gmail.com",Message.Type.chat);
msg.setBody("ssup?");
connection.sendPacket(msg);
Use this to send IM to some user.

presence.setStatus(status);
connection.sendPacket(presence);
Use this to change status.
There are many fun things that you can do with this. You can make your status rotate, write the lyrics of your favourite song line by line, flood, or change your status from busy to available and vice-versa continuously to get a alternating red-green thing.
Do your thing and please share it. Will love to learn from you.

No comments:

Post a Comment