openfire 中的会议室不像QQ群一样,不能保存那些离线用户,加入会议室后,一旦断开连接,就会离开会议室。
虽然如此,但如果要实现也不是不可能,我们可以自己做后台来保存,有兴趣的可以去试着实现一下,这里就不实现了。
好了,下面我们就一起来学习一下创建会议室、加入会议室以及查询会议室中的所有成员:
一、创建会议室
注释都写的很清楚,不做太多解释。
需要配置更多信息,可以到 中15.5.3 muc#roomconfig FORM_TYPE 查看配置
里面还有一些关于会议室的信息,感兴趣的可以看看,不过里面都是英文,不懂英文的可以谷歌翻译,楼主就是这么干的~
-
-
-
-
-
-
public static void createRoom(String roomName) {
-
if (connection == null) {
-
return;
-
}
-
try {
-
-
MultiUserChat muc = new MultiUserChat(connection, roomName
-
+ "@conference." + connection.getServiceName());
-
-
muc.create(roomName);
-
-
Form form = muc.getConfigurationForm();
-
-
Form submitForm = form.createAnswerForm();
-
-
for (Iterator fields = form.getFields(); fields
-
.hasNext();) {
-
FormField field = (FormField) fields.next();
-
if (!FormField.TYPE_HIDDEN.equals(field.getType())
-
&& field.getVariable() != null) {
-
-
submitForm.setDefaultAnswer(field.getVariable());
-
}
-
}
-
-
List owners = new ArrayList();
-
owners.add(connection.getUser());
-
submitForm.setAnswer("muc#roomconfig_roomowners", owners);
-
-
submitForm.setAnswer("muc#roomconfig_persistentroom", false);
-
-
submitForm.setAnswer("muc#roomconfig_membersonly", false);
-
-
submitForm.setAnswer("muc#roomconfig_allowinvites", true);
-
-
-
-
-
-
-
-
submitForm.setAnswer("muc#roomconfig_enablelogging", true);
-
-
submitForm.setAnswer("x-muc#roomconfig_reservednick", true);
-
-
submitForm.setAnswer("x-muc#roomconfig_canchangenick", false);
-
-
submitForm.setAnswer("x-muc#roomconfig_registration", false);
-
-
submitForm.setAnswer("muc#roomconfig_passwordprotectedroom", true);
-
-
muc.sendConfigurationForm(submitForm);
-
} catch (XMPPException e) {
-
e.printStackTrace();
-
}
-
}
二、加入会议室
-
-
-
-
-
-
-
-
-
public static MultiUserChat joinMultiUserChat(String user, String password, String roomsName,
-
XMPPConnection connection) {
-
try {
-
-
MultiUserChat muc = new MultiUserChat(connection, roomsName
-
+ "@conference." + connection.getServiceName());
-
-
DiscussionHistory history = new DiscussionHistory();
-
history.setMaxStanzas(0);
-
-
-
muc.join(user, password, history, SmackConfiguration.getPacketReplyTimeout());
-
System.out.println("会议室加入成功........");
-
return muc;
-
} catch (XMPPException e) {
-
e.printStackTrace();
-
System.out.println("会议室加入失败........");
-
return null;
-
}
-
}
三、查询会议室中的所有成员
注意:这里查询出来的名字只是用户进入会议室所用的昵称
-
-
-
-
-
public static List findMulitUser(MultiUserChat muc){
-
List listUser = new ArrayList();
-
Iterator it = muc.getOccupants();
-
-
while (it.hasNext()) {
-
-
String name = StringUtils.parseResource(it.next());
-
listUser.add(name);
-
}
-
return listUser;
-
}
四、获取服务器上所有会议室
-
-
-
-
-
-
public static List getConferenceRoom() throws XMPPException {
-
List list = new ArrayList();
-
new ServiceDiscoveryManager(connection);
-
if (!MultiUserChat.getHostedRooms(connection,
-
connection.getServiceName()).isEmpty()) {
-
-
for (HostedRoom k : MultiUserChat.getHostedRooms(connection,
-
connection.getServiceName())) {
-
-
for (HostedRoom j : MultiUserChat.getHostedRooms(connection,
-
k.getJid())) {
-
RoomInfo info2 = MultiUserChat.getRoomInfo(connection,
-
j.getJid());
-
if (j.getJid().indexOf("@") > 0) {
-
-
FriendRooms friendrooms = new FriendRooms();
-
friendrooms.setName(j.getName());
-
friendrooms.setJid(j.getJid());
-
friendrooms.setOccupants(info2.getOccupantsCount());
-
friendrooms.setDescription(info2.getDescription());
-
friendrooms.setSubject(info2.getSubject());
-
list.add(friendrooms);
-
}
-
}
-
}
-
}
-
return list;
-
}
五、监听会议室的消息
添加会议室消息监听 :
-
multiChat.addMessageListener(new multiListener());
-
-
-
-
-
-
-
public class multiListener implements PacketListener {
-
@Override
-
public void processPacket(Packet packet) {
-
Message message = (Message) packet;
-
-
String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
-
MucHistory mh = new MucHistory();
-
mh.setUserAccount(account);
-
String from = StringUtils.parseResource(message.getFrom());
-
String fromRoomName = StringUtils.parseName(message.getFrom());
-
mh.setMhRoomName(fromRoomName);
-
mh.setFriendAccount(from);
-
mh.setMhInfo(message.getBody());
-
mh.setMhTime(time);
-
mh.setMhType("left");
-
-
}
-
}
六、发送会议室消息
-
multiChat.sendMessage(str);
七、监听会议室状态(成员的进入、离开等)
添加监听事件:
-
multiChat.addParticipantStatusListener(new ParticipantStatus());
会议室状态监听事件:
-
-
-
-
-
-
-
class ParticipantStatus implements ParticipantStatusListener {
-
-
@Override
-
public void adminGranted(String arg0) {
-
-
-
}
-
-
@Override
-
public void adminRevoked(String arg0) {
-
-
-
}
-
-
@Override
-
public void banned(String arg0, String arg1, String arg2) {
-
-
-
}
-
-
@Override
-
public void joined(String participant) {
-
System.out.println(StringUtils.parseResource(participant)+ " has joined the room.");
-
}
-
-
@Override
-
public void kicked(String arg0, String arg1, String arg2) {
-
-
-
}
-
-
@Override
-
public void left(String participant) {
-
-
System.out.println(StringUtils.parseResource(participant)+ " has left the room.");
-
-
}
-
-
@Override
-
public void membershipGranted(String arg0) {
-
-
-
}
-
-
@Override
-
public void membershipRevoked(String arg0) {
-
-
-
}
-
-
@Override
-
public void moderatorGranted(String arg0) {
-
-
-
}
-
-
@Override
-
public void moderatorRevoked(String arg0) {
-
-
-
}
-
-
@Override
-
public void nicknameChanged(String participant, String newNickname) {
-
System.out.println(StringUtils.parseResource(participant)+ " is now known as " + newNickname + ".");
-
}
-
-
@Override
-
public void ownershipGranted(String arg0) {
-
-
-
}
-
-
@Override
-
public void ownershipRevoked(String arg0) {
-
-
-
}
-
-
@Override
-
public void voiceGranted(String arg0) {
-
-
-
}
-
-
@Override
-
public void voiceRevoked(String arg0) {
-
-
-
}
-
-
}
阅读(2411) | 评论(0) | 转发(0) |