XMPP 中节 ID 的用途是什么?

What is the purpose of Stanza IDs in XMPP?

谁能解释一下 ID 在某些 XMPP XML 请求中的作用(谈论核心 XMPP)?

例如,我可以通过发送这个请求来获取花名册:

<iq type="get">
    <query xmlns="jabber:iq:roster"></query>
</iq>

但我也可以通过添加随机 ID 来获取它:

<iq type="get" id="2346543">
    <query xmlns="jabber:iq:roster"></query>
</iq>

我应该或不应该使用 ID 有什么理由吗?与安全有关吗?有什么缺点吗?关于 ID 的约定是什么?应该如何构造?

当您发送带有 type="get"type="set" 的 IQ 时,您将收到type="result" 和相同 id

的响应

所以这只是为了通过 id 匹配对他们请求的响应。

例如,

获取花名册:

<iq type="get" id="2346543">
    <query xmlns="jabber:iq:roster"></query>
</iq>

回复:

<iq to='icq.exampel.com' from='juliet@example.com' type='result' id='2346543'>
  <query xmlns='jabber:iq:roster'>
    <item jid='123456789@icq.example.com'
          name='Romeo'
          subscription='both'>
      <group>Friends</group>
    </item>
    <item jid='554323654@icq.example.com'
          name='Mercutio'
          subscription='from'>
      <group>Friends</group>
    </item>
    <item jid='997665667@icq.example.com'
          name='Benvolio'
          subscription='both'>
      <group>Friends</group>
    </item>
  </query>
</iq>

它们包含相同的 ID,因此您知道此响应的请求是什么

我建议简单地查看协议规范。 RFC 6120 § 8.1.3. id 解释得很好恕我直言:

The 'id' attribute is used by the originating entity to track any
response or error stanza that it might receive in relation to the
generated stanza from another entity (such as an intermediate server
or the intended recipient).

It is up to the originating entity whether the value of the 'id'
attribute is unique only within its current stream or unique
globally.

For <message/> and <presence/> stanzas, it is RECOMMENDED for the
originating entity to include an 'id' attribute; for <iq/> stanzas,
it is REQUIRED.

If the generated stanza includes an 'id' attribute then it is
REQUIRED for the response or error stanza to also include an 'id'
attribute, where the value of the 'id' attribute MUST match that of
the generated stanza.

并不是说您的示例显示了没有 'id' 属性集的 IQ 节实际上是 无效