XMPP用户登陆

XMPP用户登陆


技术博客http://www.cnblogs.com/ChenYilong/  
新浪微博http://weibo.com/luohanchenyilong  



 















XMPP 用户登陆
 

技术博客 http://www.cnblogs.com/ChenYilong/ 新浪微博 http://weibo.com/luohanchenyilong
 


XMPP核心文件,基于TCPXML流的传输 

  XMPPStream :是开发过程当中最主要交互的类,全部扩展和自定义代 
码均要基于此类进行 
  XMPPParser :供 XMPPStream 解析使用 
  XMPPJID :提供了一个不可变 JID 的实现,遵照 NSCopying 协议和  NSCoding 协议 
  XMPPElement :如下三个 XMPP 元素的基类 
  XMPPIQ : 请求 ( 加好友 ) 
  XMPPMessage : 消息 
  XMPPPresence : 出席 ( 标示用户的在线状态 ) 
  XMPPModule :开发 XMPP 扩展时使用 
  XMPPLogging : XMPP 的日志框架 
  XMPPInternal :整个 XMPP 框架内部使用的核心和高级底层内容 



XMPP 用户登陆的实现步骤

  XMPPFrame 框架是经过代理的方式实现消息传递的 
  实现用户登陆的步骤以下: 
  1.  实例化 XMPPStream 并设置代理,同时添加代理到工做队列 
  2.  使用 JID 链接至服务器 ,默认端口为 5222 , JID 字符串中须要包含服 务器的域名 
  3.  完成链接的代理 方法中验证用户密码,链接完成后 XMPPStream  isConnect 属性为 YES 
  4.  验证代理方法 中判断用户是否登陆成功 
  5.  上线或者下线成功后,向服务器发送 Presence 数据,以更新用户在 服务器的状态 


AppDelegate.h

  为了简化开发, XMPP 的引用程序一般会将 XMPPStream 放置在
AppDelegate 中,以便于全局访问   AppDelegate 中添加如下属性和方法定义
@property  ( strong ,  nonatomic ,  readonly ) XMPPStream  *xmppStream; - ( void )connect;
- (
void )disconnect; 


XMPPStream私有方法--设置代理及通知状态

// 设置 XMPPStream
- ( void )setupStream { 
_xmppStream  = [[ XMPPStream  alloc ]  init ]; 
[ _xmppStream  addDelegate : self delegateQueue : dispatch_get_global_queue ( DISPATCH_QUEUE_PRIORITY_DEFAULT , 0 )]; 

} 
// XMPPStream 上线   - ( void )goOnline  XMPPPresence [ _xmppStream
} 
{
*presence = [
XMPPPresence  presence ]; sendElement :presence]; 
// XMPPStream 离线 
- ( void )goOffline { 
XMPPPresence  *presence = [ XMPPPresence presenceWithType : @"unavailable" ]; 
[ _xmppStream  sendElement :presence]; } 

connect 方法

- ( void )connect {
// 1.  设置 XMPPStream  [ self  setupStream ];
// 2.  设置用户名、密码及服务器   // 3.  设置 XMPPStream 信息 
[ _xmppStream  setMyJID :[ XMPPJID jidWithString :userName]]; [ _xmppStream setHostName :server]; 
_myPassword  = password;
// 4.  链接至服务器,若是没有指定 jid hostName ,链接才会报错 
NSError  *error =  nil ;
[
_xmppStream connectWithTimeout : XMPPStreamTimeoutNone error :&error];  if  (error) { 
NSLog ( @" 链接错误: %@" , error. localizedDescription ); } 
} 


disconnect 方法

- ( void )disconnect { 
// 1.  发送离线状态  [ self  goOffline ];
// 2. XMPPStream 断开链接  [ _xmppStream disconnect ]; 
} 


XMPP 代理方法

#pragma mark - XMPPStream 代理方法 
#pragma mark
  链接到服务器 
- ( void )xmppStreamDidConnect:( XMPPStream  *)sender { 
// 1.  验证密码  NSError  *error =  nil ; 
[ _xmppStream  authenticateWithPassword : _myPassword error :&error];  if  (error) { 
NSLog ( @" 身份验证错误: %@" , error. localizedDescription ); } 
} 
#pragma mark  经过验证 
- ( void )xmppStreamDidAuthenticate:( XMPPStream  *)sender { 
[ self  goOnline ]; } 
#pragma mark  验证失败 
- ( void )xmppStream:( XMPPStream  *)sender didNotAuthenticate:( DDXMLElement  *)error { 
NSLog ( @" 验证失败: %@" , error); } 


在应用程序状态切换时链接或断开链接

#pragma mark  应用程序注销激活状态  - ( void )applicationWillResignActive:( UIApplication  *)application { 
//  断开链接 
[ self  disconnect ]; } 

#pragma mark  应用程序从新被激活 
- ( void )applicationDidBecomeActive:( UIApplication  *)application { 
//  链接 
[ self  connect ]; } 


AppDelegate中添加访问助手方法

#pragma mark - AppDelegate 访问助手方法  /** 
AppDelegate 的访问助手 
*/ 
- (
AppDelegate  *)appDelegte { 
return  ( AppDelegate  *)[[ UIApplication sharedApplication ]  delegate ]; 
} 
/** AppDelegate XMPPStream 属性访问助手  */ 
- ( XMPPStream  *)xmppStream { 
return  [[ self  appDelegte ]  xmppStream ]; } 


©  chenyilong. Powered by  Postach.io
相关文章
相关标签/搜索