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
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With