连接中关联如下:
- // iq提供者
- roviderManager.getInstance().addIQProvider("ping", "urn:xmpp:ping",
- new PingIQProvider());
- onnection.addPacketListener(new PacketListener() {
- @Override
- public void processPacket(Packet p) {
- PingIQ pingIQ = (PingIQ) p;
- IQ pongIQ = pingIQ.createResultIQ(pingIQ);
- // 返回
- connection.sendPacket(pongIQ);
- }
- , new PacketTypeFilter(PingIQ.class));
两个关联类,PingIQ,PingIQProvider
- import org.jivesoftware.smack.packet.IQ;
- public class PingIQ extends IQ {
- public static final String ELEMENT = "ping";
- public static final String NAMESPACE = "urn:xmpp:ping";
- @Override
- public String getChildElementXML() {
- StringBuffer sb = new StringBuffer();
- sb.append("<").append(ELEMENT).append(" xmlns=\"").append(NAMESPACE)
- .append("\">");
- sb.append("</").append(ELEMENT).append(">");
- return sb.toString();
- }
- }
- import org.jivesoftware.smack.packet.IQ;
- import org.jivesoftware.smack.provider.IQProvider;
- import org.xmlpull.v1.XmlPullParser;
- public class PingIQProvider implements IQProvider {
- @Override
- public IQ parseIQ(XmlPullParser parser) throws Exception {
- // TODO Auto-generated method stub
- PingIQ iq = new PingIQ();
- return iq;
- }
- }
报文如下:
-
- <iq xmlns="jabber:client" type="get" id="65dfds" from="localhost" to="test001@xmpp.qxun.org/test"><ping xmlns="urn:xmpp:ping"/></iq>
- <iq type="result" to="localhost" id="65dfds"/>
http://linyu19872008.iteye.com/blog/1840596