现在的位置: 首页 > java > openfire > 正文
openfire spark自定义iq 客户端发送 服务端接受成功
2012年05月16日 openfire ⁄ 共 4920字 暂无评论

客户端两个类 一个是 plugin类  一个是定义了一个iq  
import org.jivesoftware.MainWindow;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.provider.IQProvider;
import org.jivesoftware.smack.provider.ProviderManager;
import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.Workspace;
import org.jivesoftware.spark.plugin.Plugin;
import org.xmlpull.v1.XmlPullParser;

/**
 * Implements the Spark Plugin framework to display the different possibilities using
 * Spark.
 */

public class ExamplePlugin implements Plugin {

    /**
     * Called after Spark is loaded to initialize the new plugin.
     */
    public void initialize() {
     
     ProviderManager providerManager = ProviderManager.getInstance();
  
   SparkManager.getConnection().sendPacket(new Broadcasts());
 
     providerManager.getInstance().addIQProvider("test", "com.perssoft.auth", //1
new IQProvider() {
   
 

   @Override
   public IQ parseIQ(XmlPullParser arg0) throws Exception {
    // TODO Auto-generated method stub
      
    System.out.println("返回值了");
    return null;
   }
  });

 

 
   
    

     }
      public void shutdown() {

    }

    /**
     * Return true if the Spark can shutdown on users request.
     * @return true if Spark can shutdown on users request.
     */
    public boolean canShutDown() {
        return true;
    }

    /**
    * Is called when a user explicitly asked to uninstall this plugin.
    * The plugin owner is responsible to clean up any resources and
    * remove any components install in Spark.
    */
    public void uninstall(){
       // Remove all resources belonging to this plugin.
    }
}

 

客户端 plugin.xml

<?xml version="1.0" encoding="UTF-8"?>

  <!-- Google Plugin Structure -->
      <plugin>
        <name>Plugin test</name>
        <class>org.jivesoftware.spark.examples.ExamplePlugin</class>
        <author>Derek DeMoro</author>
        <version>1.0</version>
        <description>Enables users to find files and emails relating to users using Google Desktop technology.</description>
        <email>ddman@jivesoftware.com</email>
        <minSparkVersion>2.0.6</minSparkVersion>
      </plugin>
 客户端另一个类 是一个iq  ,用来发送给客户端的

import org.jivesoftware.smack.packet.IQ;

public class Broadcasts extends IQ {   
   
    private String body="nihao ";   
    public String getElementName() {   
        return "test";   
    }   
   
    public String getNamespace() {   
        return "com.perssoft.auth";   
    }   
   
    public void setBody(String body) {   
        this.body = body;   
    }   
   
    public String getBody() {   
        return body;   
    }   
   
    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(">");   
        System.out.println(sb.toString());
        return sb.toString();   
    }   
   
}   

 

 

服务端同样两个类; 一个是plugin类

package com.perssoft.plugin;

import java.io.File;

import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.container.Plugin;
import org.jivesoftware.openfire.container.PluginManager;

public class Iqtest  implements Plugin{

    private XMPPServer server;

 @Override
 public void destroyPlugin() {
  // TODO Auto-generated method stub
  
 }

 @Override
 public void initializePlugin(PluginManager manager, File pluginDirectory) {
 
        server = XMPPServer.getInstance();
        System.out.println("<><><>:iqtest success");
        server.getInstance().getIQRouter().addHandler(new TestTemplateMethodHandler()); //1
   

    }

}

 

另一个是 iqhandler 类

package com.perssoft.plugin;
import org.dom4j.DocumentHelper;
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 TestTemplateMethodHandler extends IQHandler
 {
     private IQHandlerInfo info;
    
     public TestTemplateMethodHandler()
     {
         super("TestTemplateMethodHandler");
         info = new IQHandlerInfo("test", "com.perssoft.auth");
     }
     @Override
     public IQHandlerInfo getInfo()
     {
         // TODO Auto-generated method stub
         return info;
     }
 
     @Override
     public IQ handleIQ(IQ packet) throws UnauthorizedException
     {
     
     
         System.out.println(packet.toXML()); 
        
 Element groups = packet.getChildElement();
  
  Element childElement = packet.getChildElement();
        String namespace = childElement.getNamespaceURI();

  Element childElementCopy = packet.getChildElement().createCopy();
 final String[] toUser = groups.elementText("host").split("@");
     //    XMPPServer.getInstance().getSessionManager().sendServerMessage(null, "gogogo!"); 
 IQ reply = IQ.createResultIQ(packet);
 reply.setType(IQ.Type.error);
    Element reason = DocumentHelper.createElement("reason");
    reason.addNamespace("", "com.perssoft.auth");
    reason.setText("test");
    reply.setChildElement(reason);
        
         return reply;
     }
 }

 

参考文档

http://blog.csdn.net/hexudong08/article/details/7460246

 

http://hi.baidu.com/ouzian/blog/item/01a3520eaa013cd47acbe1f7.html

给我留言

您必须 [ 登录 ] 才能发表留言!

×