Message:
接着写
.h
1/*!2* @Author Dylan.3*4* callback Block5*/6typedefvoid(^sendSuccess)();7typedefvoid(^sendFailure)(id);89/*!10* sendMessageBy model11*/12- (void)sendMessage: (ADMessageModel *)message13sendSuccess: (sendSuccess)success14sendFailure: (sendFailure)failure;1516/*!17* @Author Dylan.18*19* unRead Msg20*/21@PRoperty (nonatomic, strong) NSMutableDictionary *unReadMsg;2223/*!24* @Author Dylan.25*26* new Msg27*/28@property (nonatomic, copy)void(^newMessage) (id);293031@end
.m
1#pragmamark - initData2- (void)initData {3//可做数据持久化4self.unReadMsg =[NSMutableDictionary dictionary];5}67#pragmamark Methods8- (void)sendMessage: (ADMessageModel *)message9sendSuccess: (sendSuccess)success10sendFailure: (sendFailure)failure {1112//set callback block13self.success =success;14self.failure =failure;1516NSxmlElement * body = [NSXMLElement elementWithName:@"body"];17[body setStringValue:message.body];1819//生成XML消息文档20NSXMLElement *mes = [NSXMLElement elementWithName:@"message"];21//消息类型22[mes addAttributeWithName:@"type"stringValue:@"chat"];23//发送给谁24[mes addAttributeWithName:@"to"stringValue:message.to];25//由谁发送26[mes addAttributeWithName:@"from"stringValue:message.from];27//组合28[mes addChild:body];29//发送消息30[[self xmppStream] sendElement:mes];31}3233#pragmamark - delegeteMethods34- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message {3536NSString * body = [[message elementForName:@"body"] stringValue];37NSString * from = [[message attributeForName:@"from"] stringValue];3839if(body !=nil) {4041NSMutableDictionary * msgDict =[NSMutableDictionary dictionary];42ADMessageModel * model =[[ADMessageModel alloc] init];43model.body =body;44model.from =from;45[msgDict setValue:model forKey:[ADCurrentTime getCurrentTime]];4647if([from isEqualToString:[[NSUserDefaults standardUserDefaults] stringForKey:CURRENT_CHAT]]) {4849self.newMessage(msgDict);50}else{51//not current chat52if([_unReadMsg.allKeys containsObject:from]) {53[_unReadMsg[from] addObject:model];54}else{55[_unReadMsg setValue:[NSMutableArray arrayWithObject:msgDict] forKey:from];56}57}5859}60}6162@end