openfire 版本3.6.4
关于插件开发的基本信息,可以参考其自带的文档页plugin-dev-guide.html
中文翻译传送门http://www.360doc.com/content/10/0707/15/1332348_37445649.shtml
- 实现功能,客户端发送一个IQ包含内容的节给服务器,服务器取得内容,然后广播给所有的在线用户
- 新定义的iq为<iq id="xxx" type="set"><b xmlns="com:message:broadcasts">输入发言内容</b></iq>
- 服务器端:
- 建立org.jivesoftware.openfire.handler.IQHandler的实现类
- package test.plugin;
- import org.dom4j.Element;
- import org.jivesoftware.openfire.IQHandlerInfo;
- import org.jivesoftware.openfire.XMPPServer;
- import org.jivesoftware.openfire.auth.UnauthorizedException;
- import org.jivesoftware.openfire.handler.IQHandler;
- import org.xmpp.packet.IQ;
- public class BroadcastsIQ extends IQHandler {
- private IQHandlerInfo info;
- public BroadcastsIQ() {
- super("用户广播模块");
- info = new IQHandlerInfo("b", "com:message:broadcasts");
- }
- @Override
- public IQHandlerInfo getInfo() {
- return info;
- }
- @Override
- public IQ handleIQ(IQ packet) throws UnauthorizedException {
- Element iq = packet.getElement();
- Element b = iq.element("b");
- String text = b.getText();
- XMPPServer.getInstance().getSessionManager().sendServerMessage(null, text);//广播信息
- return null;
- }
- }
- 建立org.jivesoftware.openfire.container.Plugin的实现类
- package test.plugin;
- import java.io.File;
- import java.util.List;
- import org.jivesoftware.openfire.XMPPServer;
- import org.jivesoftware.openfire.container.Plugin;
- import org.jivesoftware.openfire.container.PluginManager;
- import org.jivesoftware.openfire.handler.IQHandler;
- public class MyPlugin implements Plugin {
- private IQHandler iQHandler;
- @Override
- public void destroyPlugin() {
- XMPPServer.getInstance().getIQRouter().removeHandler(iQHandler);
- System.out.println("插件停止成功");
- }
- @Override
- public void initializePlugin(PluginManager manager, File pluginDirectory) {
- iQHandler = new BroadcastsIQ();
- XMPPServer.getInstance().getIQRouter().addHandler(iQHandler);
- System.out.println("插件运行成功");
- }
- }
- 建立plugin.xml文件
- <?xml version="1.0" encoding="UTF-8"?>
- <plugin>
- <class>test.plugin.MyPlugin</class>
- <name>Broadcasts messages</name>
- <description>This is an Broadcasts messages plugin.</description>
- <author>me</author>
- <version>1.0</version>
- <date>01/01/2011/date>
- <url>none</url>
- <minServerVersion>3.0.0</minServerVersion>
- <licenseType>gpl</licenseType>
- <adminconsole>
- </adminconsole>
- </plugin>
- 打包为任意名称的jar文件
- 结构如下
- test.jar
- --classes
- --test
- --plugin
- --BroadcastsIQ.class
- --MyPlugin.class
- --META-INF
- --MANIFEST.MF
- --plugin.xml
- 然后把打好包的jar文件放到openfire的plugins目录下,openfire会自动加载,观察控制台是否输出 插件运行成功
- 客户端:
- 建立org.jivesoftware.smack.packet.IQ的实现类
- package test.xmpp;
- import org.jivesoftware.smack.packet.IQ;
- public class Broadcasts extends IQ {
- private String body;
- public String getElementName() {
- return "b";
- }
- public String getNamespace() {
- return "com:message:broadcasts";
- }
- public void setBody(String body) {
- this.body = body;
- }
- public String getBody() {
- return body;
- }
- @Override
- public String getChildElementXML() {
- if(getBody() == null){
- throw new RuntimeException("Broadcasts body is empty");
- }
- StringBuilder sb = new StringBuilder();
- sb.append("<").append(getElementName()).append(" xmlns=\"").append(getNamespace()).append("\">").append(getBody()).append("</").append(getElementName()).append(">");
- return sb.toString();
- }
- }
- 然后在程序中
- Broadcasts b = new Broadcasts();
- b.setBody("测试广播内容");
- b.setType(IQ.Type.SET);
- conn.sendPacket(b);