XMPP development tutorial (5)-send and receive messages

Source: Internet
Author: User

This mainly refers to the user's status update, receiving a message, and sending a message.

First, provide the core code

User online/offline notifications

 1 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 2 #pragma mark ReceivePresence 3 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 4  5 - (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence { 6     NSString *presenceType = [presence type]; 7     NSString *presenceFromUser = [[presence from] user]; 8     NSLog(@"presenceFromUser:%@",presenceFromUser); 9     NSLog(@"presenceType:%@",presenceType);10     if (![presenceFromUser isEqualToString:[[sender myJID] user]]) {11         if ([presenceType isEqualToString:@"available"]) {12             //13         } else if ([presenceType isEqualToString:@"unavailable"]) {14             //15         }16     }17 }

 

Receive messages

 1 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 2 #pragma mark ReceiveMessage 3 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 4  5 - (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message { 6     NSString *messageBody = [[message elementForName:@"body"] stringValue]; 7     NSLog(@"body:%@",messageBody); 8     NSString *replyMessage=[NSString stringWithFormat:@"replay%@",messageBody]; 9     if(![messageBody isEqual:NULL])10        [self sendMessage:replyMessage toUser:@"fengyun"];11 }

 

Send message

 1 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 2 #pragma mark sendMessage 3 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 4  5 - (void)sendMessage:(NSString *) message toUser:(NSString *) user { 6     NSXMLElement *body = [NSXMLElement elementWithName:@"body"]; 7     [body setStringValue:message]; 8     NSXMLElement *message1 = [NSXMLElement elementWithName:@"message"]; 9     [message1 addAttributeWithName:@"type" stringValue:@"chat"];10     NSString *to = [NSString stringWithFormat:@"%@@helloworldtekimac-mini.local", user];11     [message1 addAttributeWithName:@"to" stringValue:to];12     [message1 addChild:body];13     [self.xmppStream sendElement:message1];14 }

 

XMPP development tutorial (5)-send and receive messages

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.