Javascript Strophe.addHandler仅从响应中读取第一个节点是否正确?
我开始学习strophe库的用法,当我使用addHandler解析响应时,它似乎只读取xml响应的第一个节点,所以当我收到这样的xml时:Javascript Strophe.addHandler仅从响应中读取第一个节点是否正确?,javascript,xmpp,strophe,Javascript,Xmpp,Strophe,我开始学习strophe库的用法,当我使用addHandler解析响应时,它似乎只读取xml响应的第一个节点,所以当我收到这样的xml时: <body xmlns='http://jabber.org/protocol/httpbind'> <presence xmlns='jabber:client' from='test2@localhost' to='test2@localhost' type='avaliable' id='5593:sendIQ'> <
<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>
它只记录:
<presence xmlns='jabber:client' from='test2@localhost' to='test2@localhost' type='avaliable' id='5593:sendIQ'>
<status/>
</presence>
我错过了什么?这是正确的行为吗?我应该添加更多的处理程序来获取其他节吗?
感谢advance似乎是当调用函数addHandler时,堆栈(包含所有要调用的处理程序的数组)在执行处理程序时清空。因此,当调用与处理程序条件匹配的节点时,堆栈将被清除,然后将找不到其他节点,因此您必须再次设置处理程序,或添加预期调用的处理程序,如下所示:
connection.addHandler(testHandler,null,"presence");
connection.addHandler(testHandler,null,"presence");
connection.addHandler(testHandler,null,"presence");
或:
可能不是最好的解决方案,但我会一直使用,直到有人给我一个更好的解决方案,不管怎样,我会发布这个解决方案来提示我正在处理的代码流
编辑
阅读中的文档时,我发现这一行:
如果要再次调用,则处理程序应返回true;返回false将在处理程序返回后删除处理程序。
因此,只需添加一行即可修复:
connection.addHandler(testHandler,null,"presence");
function testHandler(stanza){
console.log(stanza);
return true;
}
马卡尔的回答是正确的 在处理程序函数中返回true,因此Strophe不会删除处理程序
connection.addHandler(testHandler,null,"presence");
function testHandler(stanza){
console.log(stanza);
connection.addHandler(testHandler,null,"presence");
}
connection.addHandler(testHandler,null,"presence");
function testHandler(stanza){
console.log(stanza);
return true;
}