How to get all user had joined the room

When I joined a persistent room,I want to get all users(nickName or JID) who had joined this room.(include case that user had left the room).

How can I do?

thanks!

1 Like

Hi,

When your application starts up, register a packet listener with the connection. If you want a history of users you would listen for presence packets and if you want the history of chat messages would listen for message packets.

// Create a filter for presence or message packets

PacketFilter orFilter = new OrFilter(new PacketTypeFilter(

Presence.class), new PacketTypeFilter(Message.class));

conn.addPacketListener(this, orFilter);

/code

Then, as soon as you join the room your listener will receive presence and message packets that represent the room history. Whether or not you receive room history and the maximum number of available history entries is dependent on how you configure the Openfire server. If this does not give you enough history information, then you could easily create an Openfire plugin to capture all possible traffic for as long as the server is running. Example code is included below:

public void processPacket(Packet packet) {

// TODO Check if the packet.getFrom() is from a multi-user chat room you already joined

if (packet instanceof Presence) {

Presence presence = (Presence) packet;

// Check if the packet has a timestamp, which means it is a history entry

if (getTimestamp(packet) == null) {

return;

}

// Get the id of the user that changed presence

String userId = findUserId(packet);

// Check if the user joined or left the room

if (presence.getType() != Presence.Type.unavailable) {

// TODO Do something with the user joined event

} else if (presence.getType() == Presence.Type.unavailable) {

// TODO Do something with the user left event

}

}

}

public String findUserId(Packet packet) {

// Extract the multi user chat extension

  PacketExtension extension = packet.getExtension("http://jabber.org/protocol/muc#user");

// Check if it is a user extension

if (extension instanceof MUCUser) {

MUCUser user = (MUCUser) extension;

// Try to extract properties for the user

MUCUser.Item item = user.getItem();

if (item != null) {

return item.getJid();

}

}

// Use the resource of the from ID as a last resort

return StringUtils.parseResource(packet.getFrom());

}

public Date getTimestamp(Packet packet) {

DelayInformation delay = (DelayInformation) packet.getExtension(“x”,

“jabber:x:delay”);

if (delay != null) {

return delay.getStamp();

}

return null;

}

/code

Chris

1 Like

Thanks for your answer!

This problem perplexed me several days,thanks again