1.首先扩展一个IQ:
源代码来自:
java代码为:
http://cvs.codehaus.org/viewrep/groovy/groovy/modules/xmlrpc/src/main...import org.jivesoftware.smack.packet.IQ;
/**
* @author John Wilson
*
*/
public class JabberRPC extends IQ {
private final String xml;
public JabberRPC(final String xml) {
this.xml = "<query xmlns='jabber:iq:rpc'>\n" + xml + "\n</query>";
}
public String getChildElementXML() {
return this.xml;
}
}
2.扩展一个IQProvider.
源代码来自:
http://cvs.codehaus.org/viewrep/groovy/groovy/modules/xmlrpc/src/main... java代码为:
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.provider.IQProvider;
import org.jivesoftware.smack.util.StringUtils;
import org.xmlpull.v1.XmlPullParser;
/**
* @author John Wilson
*
*/
public class JabberRPCProvider implements IQProvider {
public IQ parseIQ(final XmlPullParser parser) throws Exception {
final StringBuffer buffer = new StringBuffer();
// skip the <query> tag by calling parser.next()
while (true) {
switch (parser.next()) {
case XmlPullParser.TEXT:
// We need to escape characters like & and <
buffer.append(StringUtils.escapeForXML(parser.getText()));
break;
case XmlPullParser.START_TAG:
buffer.append('<' + parser.getName() + '>');
break;
case XmlPullParser.END_TAG:
if ("query".equals(parser.getName())) {
// don't save the </query> end tag
return new JabberRPC(buffer.toString().trim());
} else {
buffer.append("</" + parser.getName() + '>');
break;
}
default:
}
}
}
}
3.在smack中注册IQProvider .
java代码为:
org.jivesoftware.smack.provider.ProviderManager.addIQProvider("query",
"jabber:iq:rpc", new JabberRPCProvider());
4.smack客户端例子:
try {
XMPPConnection con = new XMPPConnection("127.0.0.1", 5222);
con.login("username", "password");
//注册IQProvider
org.jivesoftware.smack.provider.ProviderManager.addIQProvider(
"query", "jabber:iq:rpc", new JabberRPCProvider());
// 监听所有的包
con.addPacketListener(new PacketListener() {
public void processPacket(Packet packet) {
if (packet instanceof org.jivesoftware.smack.packet.IQ) {
org.jivesoftware.smack.packet.IQ iq =
(org.jivesoftware.smack.packet.IQ) packet;
System.out.println("rpc内容为 " + iq.getChildElementXML());
}
}
}, new org.jivesoftware.smack.filter.PacketFilter() {
public boolean accept(Packet arg0) {
return true;
}
});
con.createChat("xmlrpc.lizongbo.im").sendMessage("测试发送!");
JabberRPC jp = new JabberRPC("xmlrpc");
jp.setType(org.jivesoftware.smack.packet.IQ.Type.SET);
jp.setTo("xmlrpc.lizongbo.im");
con.sendPacket(jp);
Thread.sleep(25000);
con.close();
} catch (Exception e) {
e.printStackTrace();
}
5.服务端回应一个xmlrpc包:
wildfire中通过插件进行响应:
public void processPacket(Packet p) {
if (p instanceof IQ) {
IQ iq =(IQ)p;
IQ niq=IQ.createResultIQ(iq);
Element responseElement=niq.setChildElement("query",
"jabber:iq:rpc");
try {
java.util.Vector v=new java.util.Vector();
v.add("lizongbo xmpp");
v.add("jabber-rpc lizongbo");
v.add("xep-0009 lizongbo");
XmlRpcResponseProcessor xrrp=new XmlRpcResponseProcessor();
String rs= new String( xrrp.encodeResponse(v, "UTF-8"));
org.dom4j.Document re=org.dom4j.DocumentHelper.parseText(rs);
responseElement.add(re.getRootElement());
componentManager.sendPacket(this, niq);
} catch (Exception e) {
e.printStackTrace();
}
}
}
6.客户端输出结果为:
rpc内容为 <query xmlns='jabber:iq:rpc'>
<methodResponse><params><param><value><array><data><value>lizongbo
xmpp</value><value>jabber-rpc lizongbo</value><value>xep-0009
lizongbo</value></data></array></value></param></params></methodResponse>
</query>