Android 基于XMPP Smack Openfire 会议室创建、加入以及查询会议室中所有成员等
openfire 中的会议室不像qq(群)一样,不能保存那些离线用户,加入会议室后,一旦断开连接,就会离开会议室。
虽然如此,但如果要实现也不是不可能,我们可以自己做后台来保存,有兴趣的可以去试着实现一下,这里就不实现了。
好了,下面我们就一起来学习一下创建会议室、加入会议室以及查询会议室中的所有成员:
一、创建会议室
注释都写的很清楚,不做太多解释。
需要配置更多信息,可以到 http://xmpp.org/extensions/xep-0045.html 中15.5.3 muc#roomconfig FORM_TYPE 查看配置
里面还有一些关于会议室的信息,感兴趣的可以看看,不过里面都是英文,不懂英文的可以谷歌翻译,楼主就是这么干的~
/** * 创建房间 * * @param roomName 房间名称 */public static void createRoom(String roomName) {if (connection == null) {return;}try {// 创建一个MultiUserChatMultiUserChat muc = new MultiUserChat(connection, roomName+ "@conference." + connection.getServiceName());// 创建聊天室muc.create(roomName); // roomName房间的名字// 获得聊天室的配置表单Form form = muc.getConfigurationForm();// 根据原始表单创建一个要提交的新表单。Form submitForm = form.createAnswerForm();// 向要提交的表单添加默认答复for (Iterator<FormField> 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<String> owners = new ArrayList<String>();owners.add(connection.getUser());// 用户JIDsubmitForm.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_passwordprotectedroom", true);// 设置进入密码//submitForm.setAnswer("muc#roomconfig_roomsecret", "password");// 能够发现占有者真实 JID 的角色// submitForm.setAnswer("muc#roomconfig_whois", "anyone");// 登录房间对话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();}}
/** * 加入会议室 * * @param user 昵称 * @param password 会议室密码 * @param roomsName 会议室名 * @param connection */ public static MultiUserChat joinMultiUserChat(String user, String password, String roomsName, XMPPConnection connection) { try { // 使用XMPPConnection创建一个MultiUserChat窗口 MultiUserChat muc = new MultiUserChat(connection, roomsName + "@conference." + connection.getServiceName()); // 聊天室服务将会决定要接受的历史记录数量 DiscussionHistory history = new DiscussionHistory(); history.setMaxStanzas(0); //history.setSince(new Date()); // 用户加入聊天室 muc.join(user, password, history, SmackConfiguration.getPacketReplyTimeout()); System.out.println("会议室加入成功........"); return muc; } catch (XMPPException e) { e.printStackTrace(); System.out.println("会议室加入失败........"); return null; } }
/** * 查询会议室成员名字 * @param muc */ public static List<String> findMulitUser(MultiUserChat muc){ List<String> listUser = new ArrayList<String>(); Iterator<String> it = muc.getOccupants(); //遍历出聊天室人员名称 while (it.hasNext()) { // 聊天室成员名字 String name = StringUtils.parseResource(it.next()); listUser.add(name); } return listUser; }
/** * 获取服务器上所有会议室 * @return * @throws XMPPException */ public static List<FriendRooms> getConferenceRoom() throws XMPPException { List<FriendRooms> list = new ArrayList<FriendRooms>(); 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());//聊天室JID friendrooms.setOccupants(info2.getOccupantsCount());//聊天室中占有者数量 friendrooms.setDescription(info2.getDescription());//聊天室的描述 friendrooms.setSubject(info2.getSubject());//聊天室的主题 list.add(friendrooms); } } } } return list; }
multiChat.addMessageListener(new multiListener());
/** * 會議室信息監聽事件 * * @author Administrator * */ 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 为聊天室对象
multiChat.addParticipantStatusListener(new ParticipantStatus());
/** * 会议室状态监听事件 * * @author Administrator * */ class ParticipantStatus implements ParticipantStatusListener { @Override public void adminGranted(String arg0) { // TODO Auto-generated method stub } @Override public void adminRevoked(String arg0) { // TODO Auto-generated method stub } @Override public void banned(String arg0, String arg1, String arg2) { // TODO Auto-generated method stub } @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) { // TODO Auto-generated method stub } @Override public void left(String participant) { // TODO Auto-generated method stub System.out.println(StringUtils.parseResource(participant)+ " has left the room."); } @Override public void membershipGranted(String arg0) { // TODO Auto-generated method stub } @Override public void membershipRevoked(String arg0) { // TODO Auto-generated method stub } @Override public void moderatorGranted(String arg0) { // TODO Auto-generated method stub } @Override public void moderatorRevoked(String arg0) { // TODO Auto-generated method stub } @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) { // TODO Auto-generated method stub } @Override public void ownershipRevoked(String arg0) { // TODO Auto-generated method stub } @Override public void voiceGranted(String arg0) { // TODO Auto-generated method stub } @Override public void voiceRevoked(String arg0) { // TODO Auto-generated method stub } }