Hi Folks,
I’‘m revisiting the age old chestnut that is presence and the retrieval of it from the chat server. I need your help guys! I’‘m writing a simple chat client, using the smack libraries and the Wildfire chat server. The database I’'m using is MySQL.
I have created 2 test users, using the admin console of Wildfire. The way I am testing it is as follows; With my first test user, I log into a Spark IM client. The presence of the Spark user is “Online”. I then start up the Java chat client that I have written and log in using the other user. The users are all subscribed correctly to each other’'s presence.
The connection is made to the server correctly, and I’'m retrieving the correct roster, with the correct number of users. I then try and retrieve the presence for each user using the roster.getPresence(rosterEntry.getUser()) method, which seems to be the standard approach. However, when I try this, the presence object that is returned from the roster.getPresence() method is, more often than not, NULL. This is incorrect, as the other user is logged in and is online. At other times a presence object containing the correct information is returned. It is very, very inconsistent.
The client I have written implements the packet listener interface, and I have have provided an implementation for the processPacket(Packet p) method. I am listening out for Presence packets. And, after the initial connection and log in have been performed, if I change the presence of the test user that is logged into the Spark IM client, the packet listener in my chat client receives the presence packet without any problems. I can also have chat conversations between my two test users without any problems.
The only problem I am having is getting the initial presence information for each user in the roster when I connect to the chat server. I saw a post from a guy called Awenkus in another thread, and he said that it can sometimes takes a moment for the presence object to be returned from a roster.getPresence() request. Even if null is returned for this, I am not seeing any subsequent packets arriving via my PacketListener.
Can anybody suggest to me a reliable solution for obtaining the presence information for each user in my roster after I perform a log in to the chat server?? And if you can, could you possibly provide a simple code example to illustrate what you mean? (I’‘d understand it a lot better). I’‘ve included the code I’‘ve written to obtain the presence for each user. I’‘m hoping the JonWrights and Awenckus’'s of this world can help me out here!! I would really appreciate any help or suggestions. Thanks guys.
// Code Sample
public void setupChat(){
this.getChatSettings();
this.connectToChatServer();
if(this.connectionEstablished) {
Roster roster = this.connection.getRoster();
if(roster != null){
System.out.println("The roster count is " + roster.getEntryCount());
if (roster.getEntryCount() > 0){
Iterator i=roster.getEntries();
while(i.hasNext()){
RosterEntry rosterEntry = (RosterEntry)i.next();
if(rosterEntry != null){
String rosterUser = rosterEntry.getUser();
System.out.println("Roster Entry User is " + rosterUser);
Presence presence = roster.getPresence(rosterUser);
if(presence != null){
System.out.println("The value of presence is " + presence);
System.out.println("The presence getStatus is " + presence.getStatus());
} else {
System.out.println(“Presence is NULL”);
}
} else {
System.out.println(“Roster Entry is NULL”);
}
}
} else {
System.out.println(“The roster is EMPTY”);
}
} else {
System.out.println(“Roster is NULL”);
}
} else {
System.out.println(“Could not establish a connection with the chat server”);
}
}