user1483756
user1483756

Reputation:

Smack Presence Doesn't Work

Actually I programming a IM service (inherited google chat) by using smack API. But when i want to print buddy list and their presences, the compile mode show all presences unavailable, but in the debug mode it shows the real availability!

My code is ...

1- create connection

public boolean openConnection() {
    ConnectionConfiguration connectionConfiguration = new ConnectionConfiguration("talk.google.com", 5222, "mail.google.com");
    this.connection = new XMPPConnection(connectionConfiguration);
    try {
        this.connection.connect();
    } catch (XMPPException e) {
        // TODO: Send Error Information To Programmer's Email Address
    }
    if(this.connection.isConnected()) {
        this.roster = this.connection.getRoster();
        this.roster.addRosterListener(new RosterListener() {
            public void entriesAdded(Collection<String> addresses) {}
            public void entriesDeleted(Collection<String> addresses) {}
            public void entriesUpdated(Collection<String> addresses) {}
            public void presenceChanged(Presence presence) {}
        });
        return true;
    }
    return false;
}

2- login

public boolean login(String jid, String password) {
    try { 
        this.connection.login(jid, password, "smack");
    } catch (XMPPException e) {
        // TODO: Send Error Information To Programmer's Email Address
    }
    if(this.connection.isAuthenticated()) return true;
    return false;
}

3- buddy list

public void buddiesList() {
    Collection<RosterEntry> rosterEntries = this.roster.getEntries();
    for(RosterEntry rosterEntry: rosterEntries) {
        System.out.println(rosterEntry.username() + " === " + this.roster.getPresence(rosterEntry.getUser()));
    }
}

4- implementation

public static void main(String args[]) {
    IMService imService = new IMService();
    imService.openConnection();
    imService.login("google account", "password");
    imService.buddiesList();
}

Upvotes: 2

Views: 2389

Answers (2)

ldd
ldd

Reputation: 460

Adding a Listener to your roster could be better:

https://www.igniterealtime.org/builds/smack/docs/latest/documentation/extensions/rosterexchange.html

Upvotes: 0

Robin
Robin

Reputation: 24262

Your RosterListener doesn't do anything. This is where you have to put code to update your roster when things like presence messages are received.

The presence you are retrieving is a snapshot in time of what the state was when it was created. To keep the state current, you have to actually code the RosterListener. This is clearly stated in the Javadoc for the getPresence() method.

Upvotes: 1

Related Questions