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
相關文章
相關標籤/搜索