这一篇应该是这个系列的最后一篇了,基本已经涵盖了开发所需要的大部分功能,想要其余的功能可以看xmppFramework的文档。
发送消息:
- (IBAction)sendButton:(id)sender {
NSString *message = self.textField.text;
if (message.length > 0) {
NSXMLElement *body = [NSXMLElement elementWithName:@"body"];
[body setStringValue:message];
NSXMLElement *mes = [NSXMLElement elementWithName:@"message"];
[mes addAttributeWithName:@"type" stringValue:@"chat"];
[mes addAttributeWithName:@"to" stringValue:self.chatUser];
[mes addAttributeWithName:@"from" stringValue:[[NSUserDefaults standardUserDefaults] stringForKey:@"1@127.0.0.1"]];
[mes addChild:body];
[[self xmppStream] sendElement:mes];
}
就是把信息放到body里面组成一个xml发送过去
接收消息:
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message{
NSString *idStr = [[message attributeForName:@"id"] stringValue];
NSString *msg = [[message elementForName:@"body"] stringValue];
NSString *from = [[message attributeForName:@"from"] stringValue];
NSString *to = [[message attributeForName:@"to"] stringValue];
}
只要有人给你发消息,就会自动执行这个函数,把数据解析一下就可以了。
写了好长时间,断断续续,终于把这个系列写完了,刚开始写的时候都没有想到能写完。
因为整个代码我是直接写在项目里面的,demo就没有,大家可以看一些其他人的demo,都差不多,就是有一些地方可能比较老,会报错,但是原理是相同的。
完结,撒花!!