openfire插件入门学习

 

我在网络上找了很多openfire插件的制作的案例  但是都是不全 以下实例来源于

http://blog.csdn.net/uohzoaix/article/details/7188019

 

非常感谢这位前辈的分享,解决了偶的燃眉之急

 

openfire 版本3.6.4

关于插件开发的基本信息,可以参考其自带的文档页plugin-dev-guide.html

中文翻译传送门http://www.360doc.com/content/10/0707/15/1332348_37445649.shtml

Java代码  收藏代码

  1. 实现功能,客户端发送一个IQ包含内容的节给服务器,服务器取得内容,然后广播给所有的在线用户 
  2. 新定义的iq为<iq id="xxx" type="set"><b xmlns="com:message:broadcasts">输入发言内容</b></iq> 
  3. 服务器端: 
  4. 建立org.jivesoftware.openfire.handler.IQHandler的实现类 
  5. package test.plugin; 
  6. import org.dom4j.Element; 
  7. import org.jivesoftware.openfire.IQHandlerInfo; 
  8. import org.jivesoftware.openfire.XMPPServer; 
  9. import org.jivesoftware.openfire.auth.UnauthorizedException; 
  10. import org.jivesoftware.openfire.handler.IQHandler; 
  11. import org.xmpp.packet.IQ; 
  12. public class BroadcastsIQ extends IQHandler { 
  13. private IQHandlerInfo info; 
  14. public BroadcastsIQ() { 
  15. super("用户广播模块"); 
  16.         info = new IQHandlerInfo("b", "com:message:broadcasts"); 
  17.     } 
  18. @Override
  19. public IQHandlerInfo getInfo() { 
  20. return info; 
  21.     } 
  22. @Override
  23. public IQ handleIQ(IQ packet) throws UnauthorizedException { 
  24.         Element iq = packet.getElement(); 
  25.         Element b = iq.element("b"); 
  26.         String text = b.getText(); 
  27.         XMPPServer.getInstance().getSessionManager().sendServerMessage(null, text);//广播信息
  28. return null; 
  29.     } 
  30. 建立org.jivesoftware.openfire.container.Plugin的实现类 
  31. package test.plugin; 
  32. import java.io.File; 
  33. import java.util.List; 
  34. import org.jivesoftware.openfire.XMPPServer; 
  35. import org.jivesoftware.openfire.container.Plugin; 
  36. import org.jivesoftware.openfire.container.PluginManager; 
  37. import org.jivesoftware.openfire.handler.IQHandler; 
  38. public class MyPlugin implements Plugin { 
  39. private IQHandler iQHandler; 
  40. @Override
  41. public void destroyPlugin() { 
  42.         XMPPServer.getInstance().getIQRouter().removeHandler(iQHandler); 
  43.         System.out.println("插件停止成功"); 
  44.     } 
  45. @Override
  46. public void initializePlugin(PluginManager manager, File pluginDirectory) { 
  47.         iQHandler = new BroadcastsIQ(); 
  48.         XMPPServer.getInstance().getIQRouter().addHandler(iQHandler); 
  49.         System.out.println("插件运行成功"); 
  50.     } 
  51. 建立plugin.xml文件 
  52. <?xml version="1.0" encoding="UTF-8"?> 
  53. <plugin> 
  54.     <class>test.plugin.MyPlugin</class> 
  55.     <name>Broadcasts messages</name> 
  56.     <description>This is an Broadcasts messages plugin.</description> 
  57.     <author>me</author> 
  58.     <version>1.0</version> 
  59.     <date>01/01/2011/date> 
  60.     <url>none</url> 
  61.     <minServerVersion>3.0.0</minServerVersion> 
  62.     <licenseType>gpl</licenseType> 
  63.     <adminconsole> 
  64.     </adminconsole> 
  65. </plugin> 
  66. 打包为任意名称的jar文件 
  67. 结构如下 
  68. test.jar 
  69. --classes 
  70.   --test  
  71.     --plugin 
  72.       --BroadcastsIQ.class
  73.       --MyPlugin.class
  74. --META-INF 
  75.   --MANIFEST.MF 
  76. --plugin.xml 
  77. 然后把打好包的jar文件放到openfire的plugins目录下,openfire会自动加载,观察控制台是否输出 插件运行成功 
  78. 客户端: 
  79. 建立org.jivesoftware.smack.packet.IQ的实现类 
  80. package test.xmpp; 
  81. import org.jivesoftware.smack.packet.IQ; 
  82. public class Broadcasts extends IQ { 
  83. private String body; 
  84. public String getElementName() { 
  85. return "b"; 
  86.     } 
  87. public String getNamespace() { 
  88. return "com:message:broadcasts"; 
  89.     } 
  90. public void setBody(String body) { 
  91. this.body = body; 
  92.     } 
  93. public String getBody() { 
  94. return body; 
  95.     } 
  96. @Override
  97. public String getChildElementXML() { 
  98. if(getBody() == null){ 
  99. throw new RuntimeException("Broadcasts body is empty"); 
  100.         } 
  101.         StringBuilder sb = new StringBuilder(); 
  102.         sb.append("<").append(getElementName()).append(" xmlns=\"").append(getNamespace()).append("\">").append(getBody()).append("</").append(getElementName()).append(">"); 
  103. return sb.toString(); 
  104.     } 
  105. 然后在程序中 
  106. Broadcasts b = new Broadcasts(); 
  107. b.setBody("测试广播内容"); 
  108. b.setType(IQ.Type.SET); 
  109. conn.sendPacket(b); 
posted @ 2013-06-08 15:12  IamThat  阅读(333)  评论(0编辑  收藏  举报