openfire ping的smack解决方案

在连接中关联如下:

                // iq提供者
		ProviderManager.getInstance().addIQProvider("ping", "urn:xmpp:ping",
				new PingIQProvider());
		connection.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="[email protected]/test"><ping xmlns="urn:xmpp:ping"/></iq>


<iq type="result" to="localhost" id="65dfds"/>

 

 

你可能感兴趣的:(openfire)