Strophe.addHandler只从响应中读取第一个节点是否正确?

mar*_*ial 5 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'>
  <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>
Run Code Online (Sandbox Code Playgroud)

使用处理程序testHandler就是这样的:

connection.addHandler(testHandler,null,"presence");
function testHandler(stanza){
  console.log(stanza);
}
Run Code Online (Sandbox Code Playgroud)

它只记录:

<presence xmlns='jabber:client' from='test2@localhost' to='test2@localhost' type='avaliable' id='5593:sendIQ'>
 <status/>
</presence>
Run Code Online (Sandbox Code Playgroud)

我错过了什么?这是一种正确的行为吗?我应该添加更多处理程序来获取其他节吗?谢谢你提前

mar*_*ial 10

似乎是当函数addHandler被调用时,堆栈(包含所有要调用的处理程序的数组)在执行处理程序时被清空.因此,当调用与处理程序条件匹配的节点时,将清除堆栈,然后将找不到其他节点,因此您必须再次设置处理程序,或者添加您希望调用的处理程序,如下所示:

 connection.addHandler(testHandler,null,"presence");
 connection.addHandler(testHandler,null,"presence");
 connection.addHandler(testHandler,null,"presence");
Run Code Online (Sandbox Code Playgroud)

要么:

 connection.addHandler(testHandler,null,"presence");
 function testHandler(stanza){
    console.log(stanza);
    connection.addHandler(testHandler,null,"presence");
 }
Run Code Online (Sandbox Code Playgroud)

可能不是最好的解决方案,但我会使用,直到有人给我一个更好的解决方案,无论如何我发布这个解决方法,以提示我正在处理的代码的流程.

编辑

阅读http://code.stanziq.com/strophe/strophejs/doc/1.0.1/files/core-js.html#Strophe.Connection.addHandler中的文档我发现这一行:

如果要再次调用,处理程序应该返回true; return false将在返回后删除处理程序.

所以只需添加一行即可修复:

 connection.addHandler(testHandler,null,"presence");
 function testHandler(stanza){
    console.log(stanza);
    return true;
 }
Run Code Online (Sandbox Code Playgroud)