mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-11-16 04:12:04 +01:00
Added ability to get participant information.
git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@1859 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
parent
36e0744c60
commit
f322df6671
1 changed files with 58 additions and 1 deletions
|
@ -54,8 +54,11 @@ package org.jivesoftware.smack;
|
||||||
|
|
||||||
import org.jivesoftware.smack.packet.Presence;
|
import org.jivesoftware.smack.packet.Presence;
|
||||||
import org.jivesoftware.smack.packet.Message;
|
import org.jivesoftware.smack.packet.Message;
|
||||||
|
import org.jivesoftware.smack.packet.Packet;
|
||||||
import org.jivesoftware.smack.filter.*;
|
import org.jivesoftware.smack.filter.*;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A GroupChat is a conversation that takes plaaces among many users in a virtual
|
* A GroupChat is a conversation that takes plaaces among many users in a virtual
|
||||||
* room. When joining a group chat, you specify a nickname, which is the identity
|
* room. When joining a group chat, you specify a nickname, which is the identity
|
||||||
|
@ -70,6 +73,7 @@ public class GroupChat {
|
||||||
private String room;
|
private String room;
|
||||||
private String nickname = null;
|
private String nickname = null;
|
||||||
private boolean joined = false;
|
private boolean joined = false;
|
||||||
|
private List participants = new ArrayList();
|
||||||
|
|
||||||
private PacketCollector messageCollector;
|
private PacketCollector messageCollector;
|
||||||
|
|
||||||
|
@ -84,9 +88,31 @@ public class GroupChat {
|
||||||
public GroupChat(XMPPConnection connection, String room) {
|
public GroupChat(XMPPConnection connection, String room) {
|
||||||
this.connection = connection;
|
this.connection = connection;
|
||||||
this.room = room;
|
this.room = room;
|
||||||
|
// Create a collector for all incoming messages.
|
||||||
PacketFilter messageFilter = new AndFilter(new FromContainsFilter(room),
|
PacketFilter messageFilter = new AndFilter(new FromContainsFilter(room),
|
||||||
new PacketTypeFilter(Message.class));
|
new PacketTypeFilter(Message.class));
|
||||||
messageCollector = connection.createPacketCollector(messageFilter);
|
messageCollector = connection.createPacketCollector(messageFilter);
|
||||||
|
// Create a listener for all presence updates.
|
||||||
|
PacketFilter presenceFilter = new AndFilter(new FromContainsFilter(room),
|
||||||
|
new PacketTypeFilter(Presence.class));
|
||||||
|
connection.addPacketListener(new PacketListener() {
|
||||||
|
public void processPacket(Packet packet) {
|
||||||
|
Presence presence = (Presence)packet;
|
||||||
|
String from = presence.getFrom();
|
||||||
|
if (presence.getType() == Presence.Type.AVAILABLE) {
|
||||||
|
synchronized (participants) {
|
||||||
|
if (!participants.contains(from)) {
|
||||||
|
participants.add(from);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (presence.getType() == Presence.Type.UNAVAILABLE) {
|
||||||
|
synchronized (participants) {
|
||||||
|
participants.remove(from);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, presenceFilter);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -142,6 +168,8 @@ public class GroupChat {
|
||||||
Presence leavePresence = new Presence(Presence.Type.UNAVAILABLE);
|
Presence leavePresence = new Presence(Presence.Type.UNAVAILABLE);
|
||||||
leavePresence.setTo(room + "/" + nickname);
|
leavePresence.setTo(room + "/" + nickname);
|
||||||
connection.sendPacket(leavePresence);
|
connection.sendPacket(leavePresence);
|
||||||
|
// Reset participant information.
|
||||||
|
participants = new ArrayList();
|
||||||
nickname = null;
|
nickname = null;
|
||||||
joined = false;
|
joined = false;
|
||||||
}
|
}
|
||||||
|
@ -150,12 +178,41 @@ public class GroupChat {
|
||||||
* Returns the nickname that was used to join the room, or <tt>null</tt> if not
|
* Returns the nickname that was used to join the room, or <tt>null</tt> if not
|
||||||
* currently joined.
|
* currently joined.
|
||||||
*
|
*
|
||||||
* @return the nickname currently being used..
|
* @return the nickname currently being used.
|
||||||
*/
|
*/
|
||||||
public String getNickname() {
|
public String getNickname() {
|
||||||
return nickname;
|
return nickname;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the number of participants in the group chat. Note: this value will only
|
||||||
|
* be accurate after joining the group chat, and may fluctuate over time.
|
||||||
|
*
|
||||||
|
* @return the number of participants in the group chat.
|
||||||
|
*/
|
||||||
|
public int getParticipantCount() {
|
||||||
|
synchronized (participants) {
|
||||||
|
return participants.size();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an Iterator (of Strings) for the list of fully qualified participants
|
||||||
|
* in the group chat. For example, "conference@chat.jivesoftware.com/SomeUser".
|
||||||
|
* Typically, a client would only display the nickname of the participant. To
|
||||||
|
* get the nickname from the fully qualified name, use the
|
||||||
|
* {@link org.jivesoftware.smack.util.StringUtils#parseResource(String)} method.
|
||||||
|
* Note: this value will only be accurate after joining the group chat, and may
|
||||||
|
* fluctuate over time.
|
||||||
|
*
|
||||||
|
* @return an Iterator for the participants in the group chat.
|
||||||
|
*/
|
||||||
|
public Iterator getParticipants() {
|
||||||
|
synchronized (participants) {
|
||||||
|
return Collections.unmodifiableList(new ArrayList(participants)).iterator();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sends a message to the chat room.
|
* Sends a message to the chat room.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in a new issue