Is it correct that Strophe.addHandler reads only the first node from the answer?

I'm starting to learn how to use the strophe library, and when I use addHandler for parsing, it seems that it only reads the first node from the XML response, so when I get this XML code:

<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> 

When using the testHandler handler:

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

These are only the logs:

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

What am I missing? is this the correct behavior? Should I add more handlers to get other stanzas? Thanks for the promotion

+5
javascript xmpp strophe
source share
2 answers

It appears that when the addHandler function is called the stack (an array containing all the handlers to be called), it is empty when the handlers are executed. Therefore, when the node method that matches the conditions of the handler is called the stack, it is cleared, and then the other nodes will not be found, so you need to install the handler again or add handlers that you expect to call as follows:

  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"); } 

may not be the best solution, but I will use it until someone gives me the best one, anyway I will post this workaround to give an idea of ​​the code stream I am dealing with.

change

Read the documentation at 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 should be called again; return false will delete the handler after it returns.

Therefore, it will be fixed by adding only the line:

  connection.addHandler(testHandler,null,"presence"); function testHandler(stanza){ console.log(stanza); return true; } 
+11
source share

The correct answer is correct.

Returns true in the handler function, so Strophe does not remove the handler.

+4
source share

All Articles