Strope.addHandler 仅从响应中读取第一个节点,这是正确的吗?

发布于 2024-09-03 04:53:11 字数 1179 浏览 2 评论 0 原文

我开始学习 strope 库的用法,当我使用 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>

使用像这样使用的处理程序 testHandler :

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

它只记录:

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

我的内容我失踪了?这是正确的行为吗?我应该添加更多处理程序来获取其他节吗? 感谢您的提前

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

滥情空心 2024-09-10 04:53:11

似乎当调用函数 addHandler 时,堆栈(包含所有要调用的处理程序的数组)在执行处理程序时被清空。因此,当调用符合处理程序条件的节点时,堆栈将被清除,然后将找不到其他节点,因此您必须再次设置处理程序,或者像这样添加您期望调用的处理程序:

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

或者:

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

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

编辑

阅读http://code.stanziq.com/stropice/stropicejs/doc/1.0.1/files/core-js.html#Stropice.Connection.addHandler 我发现了这一行:

< strong>如果要再次调用处理程序,则应返回 true;返回 false 将在返回后删除处理程序。

因此,只需添加一行即可修复此问题:

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

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;
 }
冷弦 2024-09-10 04:53:11

马克西亚的回答是正确的。

在处理函数中返回 true,因此 Strope 不会删除该处理程序。

markcial's answer is right.

Return true in the handler function, so Strophe will not remove the handler.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文