AnsweredAssumed Answered

How to write a listener to listen namespace: jabber:iq:roster

Question asked by Soosky on Dec 2, 2015
Latest reply on Dec 6, 2015 by dengd

Hi, everyone:

 

   Below code to login XMPP Server. when i call conn.login(), will send a IQ stanza(<iq><query xmlns='jabber:iq:roster'/></iq>) to server to require roster list.

I want to write a listener to listen when roster IQ(type='result') stanza return.

    

   I just know how to write a listener to listen IQ stanza like this :StanzaFilter stanzaFilter = new StanzaTypeFilter(IQ.class);

 

  Below is my code:

 

  XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();

  configBuilder.setDebuggerEnabled(true)

  .setUsernameAndPassword(username, password)

  .setServiceName(serviceName)

  .setPort(port)

  .setResource(resource)

  .setSecurityMode(SecurityMode.disabled);

 

  SASLAuthentication.registerSASLMechanism(new SASLMechanism() {

 

  @Override

  protected SASLMechanism newInstance() {

  return this;

  }

 

  @Override

  public int getPriority() {

  return 0;

  }

 

  @Override

  public String getName() {

  return null;

  }

 

  @Override

  protected byte[] getAuthenticationText() throws SmackException {

  return null;

  }

 

  @Override

  public void checkIfSuccessfulOrThrow() throws SmackException {

 

  }

 

  @Override

  protected void authenticateInternal(CallbackHandler cbh) throws SmackException {

 

  }

  });

 

  try {

  conn = new XMPPTCPConnection(configBuilder.build());

  // connect

  conn.connect();

 

  // add roster listener

  roster = Roster.getInstanceFor(getConn());

  roster.addRosterListener(rosterListener);

 

  /*

     How to write a listenr to listen iq stanza which sub-element named query with namespace "iq:jabber:roster", as below:

    <iq type="result" id="Y8P5d-6" to="wangke@192.168.9.115/SooChat">

                    <query xmlns="jabber:iq:roster">

                        <item jid="liangye@127.0.0.1" subscription="both"/>

                        <item jid="wangsy@127.0.0.1" subscription="none"/>

                        <item jid="wangying@127.0.0.1" subscription="to"/>

                        <item jid="wangzc@127.0.0.1" subscription="to"/>

                        <item jid="sung@127.0.0.1" subscription="to"/>

                    </query>

                </iq>

*/

  StanzaFilter stanzaFilter = new StanzaTypeFilter(IQ.class);

  StanzaListener myListener = new StanzaListener() {

       @Override

       public void processPacket(Stanza stanza) throws NotConnectedException {

            String from = stanza.getFrom();

            String to = stanza.getTo();

            System.out.println("from=" + from + " to=" + to + " stanza=" + stanza);

 

            // Will initialize roster list....

       } 

  

  };

  conn.addAsyncStanzaListener(myListener, stanzaFilter);

 

  // login

  conn.login();

Outcomes