help
PingManager (Smack 4.1.4 API)
it used client to server; but I want listen the ping from server to client
it used client to server; but I want listen the ping from server to client.
my android client can’t receive message after a minute . maybe i can’t receive ping from server
how can i listen the ping of server to client .
con.addAsyncStanzaListener(new StanzaListener() {
@Override
public void processPacket(Stanza stanza) throws SmackException.NotConnectedException {
Log.e(“Smack3”, “收到ping”);
}
}, new StanzaFilter() {
@Override
public boolean accept(Stanza stanza) {
if (IQ.class.isInstance(stanza)) {
Log.e(“Smack3”, “收到IQ”);
IQ iq = (IQ) stanza;
XmlStringBuilder ElementXML = iq.toXML();
Log.e(“Smack3”, ElementXML.toString());
if (ElementXML != null && ElementXML.toString().contains(“urn:xmpp:ping”)) {
return true;
}
}
return false;
}
});
the log is not used,
Using an AndFilter with StanzaTypeFilter using Ping.class and a IQTypeFilter.RESULT would be the Smack idiomatic approach.
my code in below still work . Is there problem in my code ? Thanks a lot.
con.addAsyncStanzaListener(pingServerListener, pingServerFilter);
class PingServerListener implements StanzaListener {
@Override
public void processPacket(Stanza stanza) throws SmackException.NotConnectedException {
Ping ping = (Ping) stanza;
if (ping != null && con != null) {
Log.e(“smack”, “send pong to server”);
con.sendStanza(ping.getPong());
}
}
}
class PingServerFilter implements StanzaFilter {
@Override
public boolean accept(Stanza stanza) {
Log.e(“smack”, “receive”);
if (Ping.class.isInstance(stanza)) {
Log.e(“smack”, “receive ping from server”);
Ping ping = (Ping) stanza;
if (ping.getType().equals(Ping.Type.get)) {
XmlStringBuilder pingXML = ping.toXML();
Log.e(“smack”, “receive ping from server”);
return (pingXML.toString().contains(“urn:xmpp:ping”));
} else {
return false;
}
}
return false;
}
}
My code can’t still work