标签:源代码 ber padding asto toc standard res protocol sla
在现阶段的通信服务中,各种标准都有,因此会出现无法实现相互连通,而XMPP(Extensible Message and presence Protocol)协议的出现。实现了整个及时通信服务协议的互通。有了这个协议之后。使用不论什么一个组织或者个人提供的即使通信服务,都可以无障碍的与其它的及时通信服务的用户进行交流。比如google 公司2005年推出的Google talk就是一款基于XMPP协议的即时通信软件。
以下我们就谈论一下怎样简单的使用XMPP的接收和发送消息
//消息存档 @property(nonatomic,strong) XMPPMessageArchiving * messageArch; //消息存档存储模型 @property(nonatomic,strong) XMPPMessageArchivingCoreDataStorage * messageStore;
AppDelegate * delgate=[UIApplication sharedApplication].delegate; //初始化头像 XMPPJID * fromJid=[XMPPJID jidWithString:self.fromJid]; NSData * fromData=[delgate.vCardAvatarModule photoDataForJID:fromJid]; self.fromImage=[[UIImage alloc] initWithData:fromData]; NSString * userName= [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"]; XMPPJID * tojid=[XMPPJID jidWithString:userName]; //设置图片模型 NSData * toData=[delgate.vCardAvatarModule photoDataForJID:tojid]; self.meImage=[[UIImage alloc] initWithData:toData]; if (self.fromImage==nil) { self.fromImage=[UIImage imageNamed:@"defalut"]; } if (self.meImage==nil) { self.meImage=[UIImage imageNamed:@"defalut"]; } [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showKey:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideKey:) name:UIKeyboardWillHideNotification object:nil]; //初始化数据存储 NSString *user= [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"]; //初始化请求 NSFetchRequest * request=[[NSFetchRequest alloc] initWithEntityName:@"XMPPMessageArchiving_Message_CoreDataObject"]; request.predicate=[NSPredicate predicateWithFormat:@"bareJidStr=%@ and streamBareJidStr=%@",self.fromJid,user]; //定义排序 NSSortDescriptor * des=[NSSortDescriptor sortDescriptorWithKey:@"timestamp" ascending:YES]; [request setSortDescriptors:@[des]]; //获取上下文 NSManagedObjectContext *context=[delgate.messageStore mainThreadManagedObjectContext]; //初始化结果存储器 fetch=[[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:context sectionNameKeyPath:nil cacheName:nil]; //设置代理 fetch.delegate=self; //開始查询 [fetch performFetch:nil];
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ id <NSFetchedResultsSectionInfo> info=fetch.sections[section]; NSLog(@"===%ld",info.numberOfObjects); return [info numberOfObjects]; } - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ XMPPMessageArchiving_Message_CoreDataObject * obj=[fetch objectAtIndexPath:indexPath]; JRChatTableViewCell * cell=nil; if (obj.isOutgoing) { cell=[tableView dequeueReusableCellWithIdentifier:@"cellto"]; }else{ cell=[tableView dequeueReusableCellWithIdentifier:@"cellfrom"]; } //设置头像 cell.image.image=self.meImage; cell.selectionStyle=UITableViewCellSelectionStyleNone; [cell setText:obj.body WithFlag:obj.isOutgoing ]; return cell; }
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showKey:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideKey:) name:UIKeyboardWillHideNotification object:nil]; - (void) showKey:(NSNotification * ) notify{ CGFloat time=[notify.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue] ; CGRect frame=[notify.userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue]; [UIView animateWithDuration:time animations:^{ self.keyView.transform=CGAffineTransformMakeTranslation(0, frame.size.height*-1); }]; } - (void) hideKey:(NSNotification * ) notify{ CGFloat time=[notify.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue] ; [UIView animateWithDuration:time animations:^{ self.keyView.transform=CGAffineTransformIdentity; }]; }
-(BOOL)textFieldShouldReturn:(UITextField *)textField{ [textField resignFirstResponder]; //发送消息 //1 取出文本 AppDelegate * delgate=[UIApplication sharedApplication].delegate; XMPPJID * jid=[XMPPJID jidWithString:self.fromJid]; //初始化消息体 XMPPMessage * message=[XMPPMessage messageWithType:@"chat" to:jid]; [message addBody:self.ketf.text]; //发送消息 [delgate.stream sendElement:message]; //将消息置空 self.ketf.text=nil; return YES; }
想要了解很多其它内容的小伙伴,能够点击查看源代码。亲自执行測试。
疑问咨询或技术交流,请增加官方QQ群: (452379712)
标签:源代码 ber padding asto toc standard res protocol sla
原文地址:http://www.cnblogs.com/wzzkaifa/p/6739964.html