XMPP Client library Smack Development of 4.0.6 Edition VI

Source: Internet
Author: User

XMPP Client library Smack Development of 4.0.6 Edition VI

Chszs, reprint need to indicate. Blog home:Http://blog.csdn.net/chszs

V. List roster and online status presence

Lists allow you to track whether other users are online, and lists allow you to organize users into groups, such as friends or working groups. Other instant Messaging IM systems treat list roster as friends list, contact list, and so on.

1. List entries

Each user in the list is represented by Rosterentry, which includes:

1) an XMPP address (e.g. "[email protected]")

2) The name of the note you wrote for the user (e.g. "Joe")

3) List of groups in the list. If the list of entries does not belong to any group, then it is called "Unfiledentry".

The following code snippet prints all the entries in the list:

Rosterroster = Connection.getroster ();

Collection<rosterentry>entries = Roster.getentries ();

for (rosterentryentry:entries) {

SYSTEM.OUT.PRINTLN (entry);

}

There is also a way to get a single entry, get the "Unfiledentry" method, and get a method of a group or all groups.

2. Online Status

Each entry in the list has an online status associated with it. The Roster.getpresence (Stringuser) method returns a Presence object that represents whether the user is online or is empty. Null is the return you have not subscribed to whether the user is online.

Note: In general, subscriptions to online status are always bound to users in the list, but this does not fit in all cases.

The online status of a user is either online or offline. When users are online, their online presence can also include extended information, such as what the user is doing, whether the user is willing to be disturbed, and so on. Refer to the presence class for details.

3. Change of listening list roster and online status presence

A typical scenario for the roster class is to display the user group and list in a tree structure, and the user list contains the status of whether the user is online. For example, refer to the roster of a EXODUSXMPP client as shown.

Information about the status of the online may change frequently, and roster entries may often be modified or deleted. To monitor changes in roster and presence data, you should use Rosterlistener. To get all the reminders for roster changes, you must register Rosterlistener before logging in to the XMPP server. The following code fragment registers a roster rosterlistener that can print any presence changes in the standard output. A standard client can use similar code to update the roster interface with changing information.

Rosterroster = Con.getroster ();

Roster.addrosterlistener (Newrosterlistener () {

Ignore event public void entriesadded (collection<string> addresses) {}

public void entriesdeleted (collection<string>addresses) {}

Public voidentriesupdated (collection<string> addresses) {}

public void presencechanged (presencepresence) {

System.out.println ("presencechanged:" + presence.getfrom () + "" + presence);

}

});

4. Add entries to roster

Roster and presence use a permissions-based model, and users must be licensed by others to add these people to roster. This protects the user's privacy and ensures that only approved users can see their presence information. So, when you want to add a user to your roster, you have to get the user to accept your request.

If a user requests to subscribe to your online status presence, the user must first add you to his roster, so he will initiate the request and you must choose to accept or reject the request. Smack handles booking requests for presence in the following three ways:

1) automatic acceptance of all presence booking requests

2) automatic rejection of all presence booking requests

3) manual processing of each presence booking request

There are three ways to set the processing of requests through the Roster.setsubscriptionmode (Intsubscriptionmode) method. Simple clients typically use the first automatic way to process a booking request, while a fully functional client should choose the third way to manually process the request, letting the end user decide whether to accept the request or reject it. If you use manual mode, you should register a packetlistener to listen for the Presence.Type.SUBSCRIBE type of presence package.


XMPP Client library Smack Development of 4.0.6 Edition VI

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.