原始地址:XMPPFrameWork IOS 开发(七)消息回执

请参考:XEP-0184协议

协议内容:

发送消息时附加回执请求

  1. <message
  2. from='[email protected]/westminster'
  3. id='richard2-4.1.247'
  4. to='[email protected]/throne'>
  5. <body>My lord, dispatch; read o'er these articles.</body>
  6. <request xmlns='urn:xmpp:receipts'/>
  7. </message>

代码实现

  1. NSString *siID = [XMPPStream generateUUID];
  2. //发送消息
  3. XMPPMessage *message = [XMPPMessage messageWithType:@"chat" to:jid elementID:siID];
  4. NSXMLElement *receipt = [NSXMLElement elementWithName:@"request" xmlns:@"urn:xmpp:receipts"];
  5. [message addChild:receipt];
  6. [message addBody:@"测试"];
  7. [self.xmppStream sendElement:message];

收到回执请求的消息,发送回执

  1. <message
  2. from='[email protected]/throne'
  3. id='bi29sg183b4v'
  4. to='[email protected]/westminster'>
  5. <received xmlns='urn:xmpp:receipts' id='richard2-4.1.247'/>
  6. </message>

代码实现

    1. - (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message
    2. {
    3. //回执判断
    4. NSXMLElement *request = [message elementForName:@"request"];
    5. if (request)
    6. {
    7. if ([request.xmlns isEqualToString:@"urn:xmpp:receipts"])//消息回执
    8. {
    9. //组装消息回执
    10. XMPPMessage *msg = [XMPPMessage messageWithType:[message attributeStringValueForName:@"type"] to:message.from elementID:[message attributeStringValueForName:@"id"]];
    11. NSXMLElement *recieved = [NSXMLElement elementWithName:@"received" xmlns:@"urn:xmpp:receipts"];
    12. [msg addChild:recieved];
    13. //发送回执
    14. [self.xmppStream sendElement:msg];
    15. }
    16. }else
    17. {
    18. NSXMLElement *received = [message elementForName:@"received"];
    19. if (received)
    20. {
    21. if ([received.xmlns isEqualToString:@"urn:xmpp:receipts"])//消息回执
    22. {
    23. //发送成功
    24. NSLog(@"message send success!");
    25. }
    26. }
    27. }
    28. //消息处理
    29. //...
    30. }
05-24 09:08