標籤:
xmpp中是如何跟伺服器進行聯絡的?
第一步:通過各種模組從伺服器擷取資料。比如電子名片模組,頭像模組,花名冊模組,訊息模組等。
第二步:修改的資料需要同步到伺服器,自己定義的方法,當點擊儲存按鈕時就調用如下方法。
#pragma mark 點擊按鈕進行伺服器更新。-(void)editVCardViewController:(WCEditVCardViewController *)editVc didFinishedSave:(id)sender{ WCLog(@"完成儲存"); //擷取當前電子名片 XMPPvCardTemp *myVCard = [WCXMPPTool sharedWCXMPPTool].vCard.myvCardTemp; //重新設定頭像 myVCard.photo = UIImageJPEGRepresentation(self.avatarImgView.image, 0.75); //重新設定myVCard裡的屬性 myVCard.nickname = self.nicknameLabel.text; myVCard.orgName = self.orgNameLabel.text; if (self.departmentLabel.text != nil) { myVCard.orgUnits = @[self.departmentLabel.text]; } myVCard.title = self.telLabel.text; myVCard.note = self.telLabel.text; //解析郵箱 //myVCard.mailer = self.emailLabel.text; if (self.emailLabel.text.length > 0) { // 只儲存一個郵箱 myVCard.emailAddresses = @[self.emailLabel.text]; } //把資料儲存到伺服器 // 內部實現資料上傳是把整個電子名片資料都從新上傳了一次,包括圖片 [[WCXMPPTool sharedWCXMPPTool].vCard updateMyvCardTemp:myVCard]; }
第三步:伺服器的內容更新後要通知用戶端進行重新整理。
方法一:不通過伺服器,將修改的內容直接顯示到用戶端。比如電子名片的修改。
方法二:通過伺服器,當伺服器儲存的內容發生改變時,xmpp架構會更新本地的資料庫內容,並且xmpp有個代理方法,當伺服器內容發生改變就會回調該方法,可以在這個方法裡進行操作從資料庫重新擷取內容,比如花名冊和聊天訊息的更改。如下:
//擷取需要的資料庫-(void)loadUsers2{ //顯示好友資料 (儲存XMPPRoster.sqlite檔案) //1.上下文 關聯XMPPRoster.sqlite檔案 NSManagedObjectContext *rosterContext = [WCXMPPTool sharedWCXMPPTool].rosterStorage.mainThreadManagedObjectContext; //2.Request 請求查詢哪張表 NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"XMPPUserCoreDataStorageObject"]; //設定排序 NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"displayName" ascending:YES]; request.sortDescriptors = @[sort]; //過濾 NSPredicate *pre = [NSPredicate predicateWithFormat:@"subscription != %@",@"none"]; request.predicate = pre; //3.執行請求 //3.1建立結果控制器 // 資料庫查詢,如果資料很多,會放在子線程查詢 // 移動用戶端的資料庫裡資料不會很多,所以很多資料庫的查詢操作都主線程 _resultsContr = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:rosterContext sectionNameKeyPath:nil cacheName:nil]; _resultsContr.delegate = self; NSError *err = nil; //3.2執行 [_resultsContr performFetch:&err]; WCLog(@"%@",_resultsContr.fetchedObjects);}
//擷取資料庫的內容並顯示-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *ID = @"ContactCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; //擷取對應的好友 //XMPPUserCoreDataStorageObject *user = self.users[indexPath.row]; XMPPUserCoreDataStorageObject *user = _resultsContr.fetchedObjects[indexPath.row]; //標識使用者是否線上 // 0:線上 1:離開 2:離線 WCLog(@"%@:線上狀態%@",user.displayName,user.sectionNum); cell.textLabel.text = user.displayName; // 1.通過KVO來監聽使用者狀態的改變 //[user addObserver:self forKeyPath:@"sectionNum" options:NSKeyValueObservingOptionNew context:nil]; switch ([user.sectionNum integerValue]) { case 0: cell.detailTextLabel.text = @"線上"; break; case 1: cell.detailTextLabel.text = @"離開"; break; case 2: cell.detailTextLabel.text = @"離線"; break; default: cell.detailTextLabel.text = @"見鬼了"; break; } //顯示好友的頭像 if (user.photo) {//預設的情況,不是程式一啟動就有頭像 cell.imageView.image = user.photo; }else{ //從伺服器擷取頭像 NSData *imgData = [[WCXMPPTool sharedWCXMPPTool].avatar photoDataForJID:user.jid]; cell.imageView.image = [UIImage imageWithData:imgData]; } return cell; }
#pragma mark -結果控制器的代理#pragma mark -資料庫內容改變調用該方法-(void)controllerDidChangeContent:(NSFetchedResultsController *)controller{ WCLog(@"%@",[NSThread currentThread]); //重新整理表格 [self.tableView reloadData];}
iOS開發:xmpp中是如何跟伺服器進行聯絡的?