Smack Presence Doesn't Work

大城市里の小女人 提交于 2020-01-04 17:51:51

问题


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();
}

回答1:


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.




回答2:


Adding a Listener to your roster could be better:

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



来源:https://stackoverflow.com/questions/11214684/smack-presence-doesnt-work

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!