powered by Jive Software

How to list users that are currently online?

I created this method for my JFrame to list users that are only online, but it seems to be listing all users that are registered with the server.

public void listUsers() throws IOException {

roster = con.getRoster();

roster.setSubscriptionMode(Roster.SUBSCRIPTION_ACCEPT_ALL);

roster.addRosterListener(new RosterListener()

{

public void entriesAdded(Collection addresses)

{

}

public void entriesDeleted(Collection addresses)

{

}

public void entriesUpdated(Collection addresses)

{

}

public void presenceChanged(String user)

{

if(roster.getPresence(user)==null)

{

//reportPresence(user,false);

System.err.println(user+" is offline");

}

else

{

System.err.println(user+ " is online");

//reportPresence(user,true);

}

}

}

);

String s=“testing”;

int n=0;

System.out.println("number of users: "+roster.getEntryCount());

System.out.println("length is "+usernames.length);

for (Iterator i=roster.getEntries(); i.hasNext(); n++) {

s=i.next().toString();

usernames[n]=s;

if(roster.getPresence(s)!=null) {

System.out.println("s is “s” and n is "+n);

usernames[n]=s;

}

}

repaint();

}

/code

BTW, the RosterListener does not work, but i’‘m not sure if I’'m using it correctly. Thank you all so much for the help and support!

Hey Phil,

you are getting slightly confused between the nature of someones Presence and there current online/offline status.

Below are four very simple classes that show a connection being made and a user logging in.

The users current Roster is then printed out to System.out. Each users presence is checked and depending on whether its null or not is then printed. The users current Presence mode is printed along side.

A very simple RosterListener is added to the Roster to listen out for presence changing events. When one occurs the roster is simply re-printed with the updated information.

1st off the Connection

public class TestXMPPConn {

private XMPPConnection conn;

private String host;

private int port;

private String username;

private String password;

public TestXMPPConn(String host, int port, String username, String password) {

this.host = host;

this.port = port;

this.username = username;

this.password = password;

}

public void connect() throws Exception {

try {

conn = new XMPPConnection(host, port);

}

catch (XMPPException e) {

throw new Exception(e.toString());

}

}

public void login() throws Exception{

if(conn.isConnected()) {

try {

conn.login(username, password);

}

catch (XMPPException e) {

throw new Exception(e.toString());

}

}

}

public XMPPConnection getConn() {

return this.conn;

}

}

/code

2nd the Roster

public class TestRoster {

private TestXMPPConn testConn;

private XMPPConnection conn;

private Roster roster;

public TestRoster() {}

public void setConnection(TestXMPPConn testConn) {

this.testConn = testConn;

conn = testConn.getConn();

roster = conn.getRoster();

roster.addRosterListener(new TestRosterListener(this));

}

public void displayRoster() {

Iterator rosterEntries = roster.getEntries();

while(rosterEntries.hasNext()) {

RosterEntry entry = (RosterEntry)rosterEntries.next();

Presence presence = roster.getPresence(entry.getUser());

if(presence != null) {

Presence.Mode mode = presence.getMode();

System.out.println(entry.getUser() + ": " + mode);

}

else {

System.out.println(entry.getUser() + “: OFFLINE”);

}

}

}

}

/code

3rd the RosterListenr

public class TestRosterListener implements RosterListener {

private TestRoster testRoster;

public TestRosterListener(TestRoster testRoster) {

this.testRoster = testRoster;

}

public void entriesAdded(Collection addresses) {}

public void entriesDeleted(Collection addresses) {}

public void entriesUpdated(Collection addresses) {}

public void presenceChanged(String user) {

testRoster.displayRoster();

}

}

/code

And finally a main to test it all out. The Thread.sleeps are just in there to allow the connection time to pull the rRoster from the server and then so the program doesnt just quit, but falls asleep allowing you time to test out the Roster updates.

public class TestAll {

public static void main(String [] args) {

TestXMPPConn testConn = new TestXMPPConn(“Your Server Here”, 5222, “Your Username Here”, “Your Password Here”);

TestRoster testRoster;

try {

testConn.connect();

}

catch(Exception e) {

System.out.println(e.toString());

}

try {

testConn.login();

Thread.sleep(1000);

testRoster = new TestRoster();

testRoster.setConnection(testConn);

testRoster.displayRoster();

}

catch(Exception e) {

System.out.println(e.toString());

}

try {

Thread.sleep(10000);

}

catch(Exception e) {}

}

}

/code

This should compile as is so hopefully will help you out but if you get stuck further just give me a shout.

Jon

Wow, that code example helps me out a lot.

I just tested it out…and it shows that everyone is OFFLINE when in fact some of them aren’'t. What is going on?

Ok this could be part of the problem:

http://www.calpoly.edu/~pchoi/spark.JPG[/img]

I have three users online, all have added each other as a “buddy”, yet some of them are shown as unavailable? I am able to chat as well.

Hmm, if I was in a similar position I’'d download a couple of different clients such as

Pandion, JBother, Gaim, PSI or Google, sign in to a few of them and then make sure that each has added the others and that they have accepted.

Also check the debug window and have a look at the Presence packets that are being sent.

If a mutal subscription exists the packet should read subscription=“both” if its not both then one of you hasnt subscribed