XMPP series (3) --- get friends list, add friends, and xmpp friends list

Source: Internet
Author: User

XMPP series (3) --- get friends list, add friends, and xmpp friends list

1. Heartbeat detection and disconnection Reconnection

Both the client and the server can set how often to send a heartbeat packet. If the other party does not return the correct pong information, the connection will be disconnected, and the reconnection function will be automatically added.

If you write your own chat function, you have to perform heartbeat detection and reconnection, Which is troublesome. Fortunately, XMPP has implemented heartbeat detection and disconnection reconnection modules. You only need to add several lines of code to achieve reconnection after disconnection, which is very convenient.

The Extensions files in XMPP are all modules that can be added by yourself. They all inherit from XMPPModule, and the addition method is very simple:

<Span style = "font-size: 18px;"> // Add a function module // 1. when sending autoPing, a stream: If the ping peer wants to indicate that he is active, a pong _ xmppAutoPing = [[XMPPAutoPing alloc] init] should be returned; // All Module modules, activate active [_ xmppAutoPing activate: self. xmppStream]; // autoPing: Because the ping request is sent regularly, the other party must return pong. Therefore, we need to set [_ xmppAutoPing setPingInterval: 1000] at this time; // not only the server responds; if it is a common user, it will respond to [_ xmppAutoPing setRespondsToQueries: YES]; // This process is C ----> S; observe S ---> C (which needs to be set on the server) // 2. autoReconnect automatically reconnects. When the connection is disconnected, it automatically reconnects and adds the last message to _ xmppReconnect = [[XMPPReconnect alloc] init]; [_ xmppReconnect activate: self. xmppStream]; [_ xmppReconnect setAutoReconnect: YES]; </span>

2. Get a friend list

You need to use the friend module to obtain the friend list and friend operations. First, add the friend module:

// 3. the Friends module supports managing, synchronizing, applying, and deleting friends _ xmppRosterMemoryStorage = [[XMPPRosterMemoryStorage alloc] init]; _ xmppRoster = [[XMPPRoster alloc] initWithRosterStorage: _ xmppRosterMemoryStorage] [_ xmppRoster activate: self. xmppStream]; // proxy [_ xmppRosterMemoryStorage and _ xmppRoster addDelegate: self delegateQueue: dispatch_get_main_queue ()] are added to both _ xmppRosterMemoryStorage, once the XMPP connection is successful, synchronize the friend to the local [_ xmppRoster setAutoFetchRoster: YES]; // automatically synchronize, and retrieve the friend from the server // turn off and automatically receive the friend request, automatically agree to [_ xmppRoster setAutoAcceptKnownPresenceSubscriptionRequests: NO] by default;

The proxy method for friend synchronization is as follows:

/*** End of synchronization *** // Method for receiving the IQ entry from the friend list and storing it in my memory-(void) xmppRosterDidEndPopulating :( XMPPRoster *) sender {[[nsicationicationcenter defacenter center] postNotificationName: kXMPP_ROSTER_CHANGE object: nil];}

Then, the Friends data is retrieved from the friends list.

# Pragma mark-notification event-(void) rosterChange {// retrieve my friends array from the memory and update the data source self. contacts = [NSMutableArray arrayWithArray: [JKXMPPTool sharedInstance]. xmppRosterMemoryStorage. unsortedUsers]; [self. tableView reloadData];}

Cell on the friend list page:
-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {UITableViewCell * cell = [tableView metadata: @ "contactCell" forIndexPath: indexPath]; XMPPUserMemoryStorageObject * user = self. contacts [indexPath. row]; UILabel * nameLabel = (UILabel *) [cell viewWithTag: 1001]; nameLabel. text = user. jid. user; UILabel * statusLabel = (UILabel *) [cell viewWithTag: 1002]; if ([user isOnline]) {statusLabel. text = @ "[Online]"; statusLabel. textColor = [UIColor blackColor]; nameLabel. textColor = [UIColor blackColor];} else {statusLabel. text = @ "[offline]"; statusLabel. textColor = [UIColor grayColor]; nameLabel. textColor = [UIColor grayColor];} return cell ;}
3. Add friends

First, add a method to JKXMPPTool:

-(Void) addFriend :( XMPPJID *) aJID {// here, nickname is my remarks on it, not in his personal data, nickname [self. xmppRoster addUser: aJID withNickname: @ "friend"];}

Then implement the proxy method of the friend module:

// If the synchronized roster is not initialized, it will be automatically stored in my friend memory-(void) xmppRosterDidChange :( XMPPRosterMemoryStorage *) sender {[[nsicationicationcenter defacenter center] postNotificationName: kXMPP_ROSTER_CHANGE object: nil];}

Processing when a friend request is received:

/** Receive the subscription request (indicating that the recipient wants to add himself as a friend) */-(void) xmppRoster :( XMPPRoster *) sender didReceivePresenceSubscriptionRequest :( XMPPPresence *) presence {NSLog (@ "didReceivePresenceSubscriptionRequest"); // adding a friend will definitely subscribe to the other party, but you do not have to add the Peer as friend self to receive the subscription. paiepresence = presence; NSString * message = [NSString stringWithFormat: @ "[% @] Want to add you as a friend", presence. from. bare]; UIAlertView * alertView = [[UIAlertView alloc] initWithTitle: nil message: message delegate: self cancelButtonTitle: @ "reject" otherButtonTitles: @ "agree", nil]; [alertView show]; // agree and add the Peer as a friend // [self. xmppRoster acceptPresenceSubscriptionRequestFrom: presence. from andAddToRoster: YES]; // method of rejection // [self. xmppRoster rejectPresenceSubscriptionRequestFrom: presence. from];}-(void) xmppStream :( XMPPStream *) sender didReceivePresence :( XMPPPresence *) presence {// receive the other party to cancel grading my message if ([presence. type isEqualToString: @ "unsubscribe"]) {// remove it from my local address book [self. xmppRoster removeUser: presence. from];}

Demo address: https://github.com/Joker-King/ChatDemo


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.