[iOS]XMPP框架_协议

XMPP_Protocol

查询服务器信息

用户通过查询服务器信息来查看服务器对特定某些功能的支持,比如:用户需要查询是否支持在多个终端同时发送消息,点击下面的查询语句,服务器返回<feature var=’urn:xmpp:carbons:2’> 节点则表明是支持此功能的。

<iq xmlns='jabber:client'
    from='romeo@montague.example/garden'
    id='info1'
    to='montague.example'
    type='get'>
  <query xmlns='http://jabber.org/protocol/disco#info'/>
  </iq>

 

服务器返回:

  <iq xmlns='jabber:client'

        from='montague.example'

        id='info1'

        to='romeo@montague.example/garden'

        type='result'>

      <query xmlns='http://jabber.org/protocol/disco#info'>

        ...

        <feature var='urn:xmpp:carbons:2'/>

        <feature var='urn:xmpp:carbons:rules:0'/>

        ...

      </query>

    </iq>

订阅房间

在多用户聊天(MUC)中,通过将房间作为PubSub公开,用户通过订阅房间的节点事件来建立和房间的关联,即使这个用户不在房间里面。也就是说用户无需向房间发送状态即可接收消息。

注意:

  1. 如果按照标准的XMPP,用户需要发送presence状态来表明自己出席了某个房间,用户加入了房间之后才能收发消息 。但是有了订阅的概念用户只需要订阅房间即可。
  2. 用户订阅房间可支持的节点事件如下:
  • urn:xmpp:mucsub:nodes:presence
  • urn:xmpp:mucsub:nodes:messages
  • urn:xmpp:mucsub:nodes:affiliations
  • urn:xmpp:mucsub:nodes:subscribers
  • urn:xmpp:mucsub:nodes:config
  • urn:xmpp:mucsub:nodes:subject
  • urn:xmpp:mucsub:nodes:system

<iq from='hag66@shakespeare.example'

    to='coven@muc.shakespeare.example'

    type='set'

    id='E6E10350-76CF-40C6-B91B-1EA08C332FC7'>

  <subscribe xmlns='urn:xmpp:mucsub:0'

             nick='mynick'

             password='roompassword'>

    <event node='urn:xmpp:mucsub:nodes:messages' />

    <event node='urn:xmpp:mucsub:nodes:affiliations' />

    <event node='urn:xmpp:mucsub:nodes:subject' />

    <event node='urn:xmpp:mucsub:nodes:config' />

  </subscribe>

</iq>

 

服务器返回:

如果订阅成功,服务器会返回如下信息。

<iq from='coven@muc.shakespeare.example'

    to='hag66@shakespeare.example'

    type='result'

    id='E6E10350-76CF-40C6-B91B-1EA08C332FC7'>

  <subscribe xmlns='urn:xmpp:mucsub:0'>

    <event node='urn:xmpp:mucsub:nodes:messages' />

    <event node='urn:xmpp:mucsub:nodes:affiliations' />

    <event node='urn:xmpp:mucsub:nodes:subject' />

    <event node='urn:xmpp:mucsub:nodes:config' />

  </subscribe>

</iq>

查询房间信息

IQ查询:

<iq from='hag66@shakespeare.lit/pda'

        id='ik3vs715'

        to='coven@chat.shakespeare.lit'

        type='get'>

      <query xmlns='http://jabber.org/protocol/disco#info'/>

    </iq>

服务器返回:

 

<iq from='coven@chat.shakespeare.lit'

        id='ik3vs715'

        to='hag66@shakespeare.lit/pda'

        type='result'>

      <query xmlns='http://jabber.org/protocol/disco#info'>

        <identity

            category='conference'

            name='A Dark Cave'

            type='text'/>

        <feature var='http://jabber.org/protocol/muc'/>

        <feature var='http://jabber.org/protocol/muc#stable_id'/>

        <feature var='muc_passwordprotected'/>

        <feature var='muc_hidden'/>

        <feature var='muc_temporary'/>

        <feature var='muc_open'/>

        <feature var='muc_unmoderated'/>

        <feature var='muc_nonanonymous'/>

      </query>

    </iq>

 

邀请订阅房间

服务器现已支持除主持人之外,普通订阅者也可以邀请别人订阅房间。

IQ查询:

 

注意:在<subscribe>节点属性意义如下:

  1. Xmlns:”urn:xmpp:mucsub:0”  固定的命名空间表明订阅房间
  2. Jid:被邀请订阅的用户的jid
  3. Nick:被邀请订阅的用户在房间的昵称;iOS目前填的是用户的jid前面部分

比如用户的jid是:13602439462@taobao.com ,则nick为:13602439462

 

<iq from='king@shakespeare.example'

    to='coven@muc.shakespeare.example'

    type='set'

    id='E6E10350-76CF-40C6-B91B-1EA08C332FC7'>

  <subscribe xmlns='urn:xmpp:mucsub:0'

             jid='hag66@shakespeare.example'

             nick='mynick'

             password='roompassword'>

    <event node='urn:xmpp:mucsub:nodes:messages' />

    <event node='urn:xmpp:mucsub:nodes:affiliations' />

    <event node='urn:xmpp:mucsub:nodes:subject' />

    <event node='urn:xmpp:mucsub:nodes:config' />

  </subscribe>

</iq>

 

取消订阅房间

  1. 订阅者自己取消房间订阅(退群)

IQ查询:

 

<iq from='hag66@shakespeare.example'

    to='coven@muc.shakespeare.example'

    type='set'

    id='E6E10350-76CF-40C6-B91B-1EA08C332FC7'>

  <unsubscribe xmlns='urn:xmpp:mucsub:0' />

</iq>

 

服务器返回:

 

<iq from='coven@muc.shakespeare.example'

    to='hag66@shakespeare.example'

    type='result'

    id='E6E10350-76CF-40C6-B91B-1EA08C332FC7' />

注意:如果是群主退群,需先通过上面的查询取消订阅,另外还要调用distory解散群。

以iOS为例:

 

<iq from='hag66@shakespeare.example'

    to='coven@muc.shakespeare.example'

    type='set'

    id='E6E10350-76CF-40C6-B91B-1EA08C332FC7'>

  <destory/>

</iq>

  1. 群主为其他成员取消订阅(踢人)

<iq from='king@shakespeare.example'

    to='coven@muc.shakespeare.example'

    type='set'

    id='E6E10350-76CF-40C6-B91B-1EA08C332FC7'>

  <unsubscribe xmlns='urn:xmpp:mucsub:0'

               jid='hag66@shakespeare.example'/>

</iq>

 

获取订阅房间列表

用户查询自己订阅的房间列表

IQ查询:

注意:to 属性指定的格式为:

subdomain.domain比如:

Subdomain:conference

Domain:taobao.com

则to为:conference.com

<iq from='hag66@shakespeare.example'

    to='muc.shakespeare.example'

    type='get'

    id='E6E10350-76CF-40C6-B91B-1EA08C332FC7'>

  <subscriptions xmlns='urn:xmpp:mucsub:0' />

</iq>

 

服务器返回:

 

<iq from='muc.shakespeare.example'

    to='hag66@shakespeare.example'

    type='result'

    id='E6E10350-76CF-40C6-B91B-1EA08C332FC7'>

  <subscriptions xmlns='urn:xmpp:mucsub:0'>

    <subscription jid='coven@muc.shakespeare.example'>

      <event node='urn:xmpp:mucsub:nodes:messages'/>

      <event node='urn:xmpp:mucsub:nodes:affiliations'/>

      <event node='urn:xmpp:mucsub:nodes:subject'/>

      <event node='urn:xmpp:mucsub:nodes:config'/>

    </subscription>

    <subscription jid='chat@muc.shakespeare.example'>

      <event node='urn:xmpp:mucsub:nodes:messages'/>

    </subscription>

  </subscriptions>

</iq>

 

获取订阅者列表

获取订阅者列表即获取房间成员。

IQ查询:

注意:直接将to属性指定为房间jid即可以查询房间成员列表。

 

<iq from='hag66@shakespeare.example'

    to='coven@muc.shakespeare.example'

    type='get'

    id='E6E10350-76CF-40C6-B91B-1EA08C332FC7'>

  <subscriptions xmlns='urn:xmpp:mucsub:0' />

</iq>

 

服务器返回:

 

<iq from='coven@muc.shakespeare.example'

    to='hag66@shakespeare.example'

    type='result'

    id='E6E10350-76CF-40C6-B91B-1EA08C332FC7'>

  <subscriptions xmlns='urn:xmpp:mucsub:0'>

    <subscription jid='juliet@shakespeare.example'>

      <event node='urn:xmpp:mucsub:nodes:messages'/>

      <event node='urn:xmpp:mucsub:nodes:affiliations'/>

    </subscription>

    <subscription jid='romeo@shakespeare.example'>

      <event node='urn:xmpp:mucsub:nodes:messages'/>

    </subscription>

  </subscriptions>

</iq>

 

2.6.3.8 查询离线消息数量

IQ查询:

 

<iq type='get'>

      <query xmlns='http://jabber.org/protocol/disco#info'

             node='http://jabber.org/protocol/offline'/>

    </iq>

 

服务器返回:

 

<iq type='result' to='romeo@montague.net/orchard'>

      <query xmlns='http://jabber.org/protocol/disco#info'

             node='http://jabber.org/protocol/offline'>

        <identity

            category='automation'

            type='message-list'/>

        <feature var='http://jabber.org/protocol/offline'/>

        <x xmlns='jabber:x:data' type='result'>

          <field var='FORM_TYPE' type='hidden'>

            <value>http://jabber.org/protocol/offline</value>

          </field>

          <field var='number_of_messages'>

            <value>66</value>

          </field>

        </x>

      </query>

    </iq>

 

离线消息收取

 

只需要发送一个prioryity 大于1的presence即可。

IQ查询:

 

<presence from='juliet@capulet.com/balcony'>

      <priority>1</priority>

    </presence>

启用carbons(消息副本)

在现实中,存在一个账号登录多个终端的情况,Carbons协议用于确保一个账号的所有设备都能获得所有对话的内容,以避免用户混淆。作为副本,在实现该协议的所有用户的客户端之间共享关于对话的当前状态的信息,确保信息在各种终端连续不间断。

 

客户端启用carbons协议只需要发送一个带<enable>节的协议即可。

IQ查询:

 

<iq xmlns='jabber:client'

        from='romeo@montague.example/garden'

        id='enable1'

        type='set'>

      <enable xmlns='urn:xmpp:carbons:2'/>

    </iq>
 

 

服务器返回:

<iq xmlns='jabber:client'

          from='romeo@montague.example'

          id='enable1'

          to='romeo@montague.example/garden'

          type='result'/>
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
XMPP协议是一种用于实现即时通讯的开放式协议。下面是使用XMPP协议的简要教程: 1. 首先,你需要搭建一个XMPP服务器,比如Openfire。你可以在网上找到很多关于Openfire搭建的教程。 2. 在前端应用中,你需要导入XMPPframework库。这个库可以帮助你与XMPP服务器进行通信。 3. 在与服务器建立连接之前,你需要建立一个XMPP流(stream)。这个流的建立过程包括协商安全性等步骤。 4. 一旦建立了XMPP流,你可以通过发送XML Stanza给服务器来进行通信。XML Stanza是一种特定格式的XML消息。 5. 服务器会根据你发送的消息和程序逻辑,向客户端发送XML Stanza。这个过程不是一问一答的,任何时候都有可能从一方发信给另外一方。 6. 通信的最后阶段是关闭流和TCP/IP连接。 总的来说,XMPP协议类似于HTTP协议,它的通信过程是一个"解包装-〉包装"的过程。你只需要理解接收到的消息类型,并理解返回的消息类型,就可以很好地利用XMPP进行数据通信。 对于开发客户端聊天工具,你可以使用Smack库。Smack是一个用Java编写的开源XMPP(Jabber)库,支持PC和移动开发。你可以在Android平台上使用Smack库来开发基于XMPP协议的即时聊天功能。 希望这个简要教程对你有所帮助。如果你需要更详细的教程,可以在网上搜索相关资源。 #### 引用[.reference_title] - *1* *2* [基于XMPP协议的即时通讯教程附Demo](https://blog.csdn.net/qq_25608527/article/details/48247427)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [即时聊天IM之三 XMPP协议客户端库的和Android端框架概述](https://blog.csdn.net/weixin_35835018/article/details/114100513)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值