Send and receive messages
Receive messages
By implementing
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message;
method
When receiving the content <message /> tag, XMPPFramework frame callback method
The XMPP protocol, message body content stored in the tag <body /> within
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message { NSString *messageBody = [[message elementForName:@"body"] stringValue]; }
Send a message
Sending a message, we need to XMPP protocol, places the data <message /> in the tag, for example:
<message type="chat" to="[email protected]">
<body>Hello World!<body />
<message />
- (void)sendMessage:(NSString *) message toUser:(NSString *) user { NSXMLElement *body = [NSXMLElement elementWithName:@"body"]; [body setStringValue:message]; NSXMLElement *message = [NSXMLElement elementWithName:@"message"]; [message addAttributeWithName:@"type" stringValue:@"chat"]; NSString *to = [NSString stringWithFormat:@"%@@example.com", user]; [message addAttributeWithName:@"to" stringValue:to]; [message addChild:body]; [self.xmppStream sendElement:message]; }
Reproduced in: https: //www.cnblogs.com/dyingbleed/archive/2013/05/16/3075105.html