powered by Jive Software

Sending iq packet from session listener

I want to send an iq packet back to the user after their session is created. I am using the following code in the sessionCreated function of the SessionEventListener.

JID user = session.getAddress();

XMPPServer server = XMPPServer.getInstance();

IQRouter iqRouter = XMPPServer.getInstance().getIQRouter();

IQ iqPacket = new IQ(IQ.Type.set);

iqPacket.setFrom(server.getServerInfo().getName());

iqPacket.setTo(user);

iqPacket.setChildElement(“query”, “clearance”);

//more code here to add elements

iqRouter.route(iqPacket);

I can see the packet in the openfire logs being sent to the correct user. But the spark client does not receive it. I have written a plugin for spark that dumps all packets to the log file so I can tell if it gets there or not. Other packets do but this one does not.

Thanks

Hi Jeff,

I’ve noticed that at times there is what appears to be some sort of timing issue when working with the SessionEventListener. I haven’'t had a chance to fully investigate the issue but a way to fix it is to get the an instance of XMPPServer, or in your case the server name and IQRouter, in the initializePlugin method of your plugin. So, something like the following should work:

public class ExamplePlugin implements Plugin {
   private ExampleSessionEventListener listener = new ExampleSessionEventListener();
   private String serverName = null;
   private IQRouter router = null;
      public void initializePlugin(PluginManager manager, File pluginDirectory) {
      SessionEventDispatcher.addListener(listener);       serverName = XMPPServer.getInstance().getServerInfo().getName();
      router = XMPPServer.getInstance().getIQRouter();
   }    public void destroyPlugin() {
      SessionEventDispatcher.removeListener(listener);
      listener = null;
             serverName = null;
      router = null;
   }    private class ExampleSessionEventListener implements SessionEventListener {
      public void sessionCreated(Session session) {
         IQ iqPacket = new IQ(IQ.Type.set);
         iqPacket.setFrom(serverName);
         iqPacket.setTo(session.getAddress());
         iqPacket.setChildElement("query", "clearance");
         //more code here to add elements
         router.route(iqPacket);
      }       public void sessionDestroyed(Session session) {
      }       public void anonymousSessionCreated(Session session) {
      }       public void anonymousSessionDestroyed(Session session) {
      }
   }
}

Hope that helps,

Ryan

Thanks for the help. Spark still isnt receiving the packet but the openfire logs now show an error packet coming back from spark. It looks like spark rejected the packet before the plugin processed it. I think that it is because I am sending a custom iq packet. After doing some searching it looks like I need to use the PacketExtension class to extend the iq packet.