1.建立一個單例模式來管理xmpp的鏈接和操做html
1 +(XMPPManager *)share 2 { 3 static XMPPManager *_share=nil; 4 static dispatch_once_t onceToken; 5 dispatch_once(&onceToken, ^{ 6 _share=[[XMPPManager alloc] init]; 7 }); 8 return _share; 9 } 10 -(id)init 11 { 12 if (self=[super init]) { 13 [self setupStream]; 14 } 15 return self; 16 } 17 - (void)setupStream 18 { 19 xmppStream = [[XMPPStream alloc] init]; 20 [xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()]; 21 [xmppStream setHostName:@"127.0.0.1"]; 22 [xmppStream setHostPort:5222]; 23 }
2.登陸和註冊都須要使用用戶名進行鏈接服務器,服務器
-(void)login { [self connect]; } -(void)reg { isReg=YES; [self connect]; } - (BOOL)connect { if (xmppStream.isConnected) { [self disconnect]; } if (![xmppStream isDisconnected]) { return YES; } if (self.userName == nil) { return NO; } [xmppStream setMyJID:[XMPPJID jidWithString:self.userName]]; NSError *error = nil; if (![xmppStream connectWithTimeout:XMPPStreamTimeoutNone error:&error]) { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error connecting" message:@"See console for error details." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alertView show]; return NO; } return YES; }
3.在鏈接代理中判斷是登陸仍是註冊,發送不一樣命令socket
//成功鏈接服務器 - (void)xmppStream:(XMPPStream *)sender socketDidConnect:(GCDAsyncSocket *)socket { DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD); } //鏈接服務器失敗 - (void)xmppStreamDidDisconnect:(XMPPStream *)sender withError:(NSError *)error { DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD); } - (void)xmppStreamDidConnect:(XMPPStream *)sender { DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD); NSError *error = nil; if (isReg) { if (![xmppStream registerWithPassword:self.pwd error:&error]) { } } else { if (![xmppStream authenticateWithPassword:self.pwd error:&error]) { } } }
4.處理登陸或註冊代理函數函數
//登陸成功 - (void)xmppStreamDidAuthenticate:(XMPPStream *)sender { DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD); [self goOnline]; UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"登陸成功" message:@"登陸成功" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alertView show]; } //登陸失敗 - (void)xmppStream:(XMPPStream *)sender didNotAuthenticate:(NSXMLElement *)error { DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD); UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"密碼錯誤" message:@"密碼錯誤" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alertView show]; } //註冊成功 - (void)xmppStreamDidRegister:(XMPPStream *)sender { DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD); UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"註冊成功" message:@"註冊成功" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alertView show]; isReg=NO; } //註冊失敗 - (void)xmppStream:(XMPPStream *)sender didNotRegister:(NSXMLElement *)error { DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD); NSLog(@"%@",[[error elementForName:@"error"] stringValue]); UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"註冊失敗" message:@"註冊失敗" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alertView show]; isReg=NO; }
5.登陸或註冊的用戶名必須「username@主機名」這種格式spa
轉載請註明:http://www.cnblogs.com/bandy/p/3756492.html代理