在smack中实现 Jabber-RPC (XEP-0009)


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>


摘在博客:http://www.blogjava.net/lizongbo/archive/2006/12/04/85433.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值