Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it right that Strophe.addHandler reads only first node from response?

I'm starting to learn strophe library usage and when i use addHandler to parse response it seems to read only first node of xml response so when i receive a xml like that :

<body xmlns='http://jabber.org/protocol/httpbind'>
 <presence xmlns='jabber:client' from='test2@localhost' to='test2@localhost' type='avaliable' id='5593:sendIQ'>
  <status/>
 </presence>
 <presence xmlns='jabber:client' from='test@localhost' to='test2@localhost' xml:lang='en'>
  <status />     
 </presence>
 <iq xmlns='jabber:client' from='test2@localhost' to='test2@localhost' type='result'>
  <query xmlns='jabber:iq:roster'>
   <item subscription='both' name='test' jid='test@localhost'>
    <group>test group</group>
   </item>
  </query>
 </iq>
</body>

With the handler testHandler used like that :

connection.addHandler(testHandler,null,"presence");
function testHandler(stanza){
  console.log(stanza);
}

It only logs :

<presence xmlns='jabber:client' from='test2@localhost' to='test2@localhost' type='avaliable' id='5593:sendIQ'>
 <status/>
</presence>

What i am missing? is it a right behaviour? Should i add more handlers to get the other stanzas? Thanks for advance

like image 424
markcial Avatar asked May 26 '10 11:05

markcial


1 Answers

Seem to be that when the function addHandler is called the stack ( an array containing all handlers to be called ) is emptyed when the handlers are executed. So when the node matching the handler conditions is called the stack is cleared and then the other nodes will not be found, so you have to set the handler again, or add the handlers you expect to be called like this :

 connection.addHandler(testHandler,null,"presence");
 connection.addHandler(testHandler,null,"presence");
 connection.addHandler(testHandler,null,"presence");

or:

 connection.addHandler(testHandler,null,"presence");
 function testHandler(stanza){
    console.log(stanza);
    connection.addHandler(testHandler,null,"presence");
 }

might not be the best solutions, but i'll use until someone gives me a better one, anyways i post this workaround to give a hint of the flow of the code i m dealing with.

edit

Reading the documentation in http://code.stanziq.com/strophe/strophejs/doc/1.0.1/files/core-js.html#Strophe.Connection.addHandler i found this line :

The handler should return true if it is to be invoked again; returning false will remove the handler after it returns.

So it will be fixed by adding only a line :

 connection.addHandler(testHandler,null,"presence");
 function testHandler(stanza){
    console.log(stanza);
    return true;
 }
like image 175
markcial Avatar answered Sep 27 '22 19:09

markcial