首页 > 代码库 > XMPP登录注册好友群组_实战教学2
XMPP登录注册好友群组_实战教学2
Message:
接着写
.h
1 /*! 2 * @Author Dylan. 3 * 4 * callback Block 5 */ 6 typedef void(^sendSuccess)(); 7 typedef void(^sendFailure)(id); 8 9 /*!10 * sendMessageBy model11 */12 - (void)sendMessage: (ADMessageModel *)message13 sendSuccess: (sendSuccess)success14 sendFailure: (sendFailure)failure;15 16 /*!17 * @Author Dylan.18 *19 * unRead Msg20 */21 @property (nonatomic, strong) NSMutableDictionary * unReadMsg;22 23 /*!24 * @Author Dylan.25 *26 * new Msg27 */28 @property (nonatomic, copy) void (^newMessage) (id);29 30 31 @end
.m
1 #pragma mark - initData 2 - (void)initData { 3 // 可做数据持久化 4 self.unReadMsg = [NSMutableDictionary dictionary]; 5 } 6 7 #pragma mark Methods 8 - (void)sendMessage: (ADMessageModel *)message 9 sendSuccess: (sendSuccess)success10 sendFailure: (sendFailure)failure {11 12 // set callback block13 self.success = success;14 self.failure = failure;15 16 NSXMLElement * body = [NSXMLElement elementWithName:@"body"];17 [body setStringValue:message.body];18 19 //生成XML消息文档20 NSXMLElement *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 }32 33 #pragma mark - delegeteMethods34 - (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message {35 36 NSString * body = [[message elementForName:@"body"] stringValue];37 NSString * from = [[message attributeForName:@"from"] stringValue];38 39 if (body != nil) {40 41 NSMutableDictionary * msgDict = [NSMutableDictionary dictionary];42 ADMessageModel * model = [[ADMessageModel alloc] init];43 model.body = body;44 model.from = from;45 [msgDict setValue:model forKey:[ADCurrentTime getCurrentTime]];46 47 if ([from isEqualToString:[[NSUserDefaults standardUserDefaults] stringForKey:CURRENT_CHAT]]) {48 49 self.newMessage(msgDict);50 } else {51 // not current chat52 if ([_unReadMsg.allKeys containsObject:from]) {53 [_unReadMsg[from] addObject:model];54 } else {55 [_unReadMsg setValue:[NSMutableArray arrayWithObject:msgDict] forKey:from];56 }57 }58 59 }60 }61 62 @end
XMPP登录注册好友群组_实战教学2
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。