/* 发送消息的格式 <message type="chat" to="hehe@example.com"> <body>Hello World!<body /> <message /> */ - (void)sendText:(NSString *)text toJid:(NSString *)jidString completion:(HYBCompletionBlock)completion { self.sendMessageBlock = completion; NSXMLElement *body = [NSXMLElement elementWithName:@"body"]; [body setStringValue:text]; NSXMLElement *message = [NSXMLElement elementWithName:@"message"]; [message addAttributeWithName:@"type" stringValue:@"chat"]; if (![jidString hasSuffix:kServer]) { jidString = [NSString stringWithFormat:@"%@@%@", jidString, kServer]; } [message addAttributeWithName:@"to" stringValue:jidString]; [message addChild:body]; [self.xmppStream sendElement:message]; }
消息节点的结构是这样的:
<message type="chat" to="hehe@example.com"> <body>Hello World!<body /> <message />
在发送后,会在代理中回调:
- (void)xmppStream:(XMPPStream *)sender didSendMessage:(XMPPMessage *)message { if (self.sendMessageBlock) { self.sendMessageBlock(YES, nil); } } - (void)xmppStream:(XMPPStream *)sender didFailToSendMessage:(XMPPMessage *)message error:(NSError *)error { if (self.sendMessageBlock) { self.sendMessageBlock(NO, [error description]); } }
结束:
话不多说了,功能简单,高级功能就不深入了,等到有机会做相关项目时,再深入研究研究。
想要源码,猛击这里:https://github.com/632840804/IOSXMPPDemoByJackyHuang