在XMPP中加入MUC房间时出错(smack)

时间:2016-06-23 06:09:31

标签: java xmpp openfire smack multiuserchat

我正在尝试创建多用户聊天。我在加入房间时遇到错误。 创建聊天室的方法:

 public void createMultiUserChatRoom(String roomName, String nickName) {

            // Get the MultiUserChatManager
            MultiUserChatManager multiUserChatManager = MultiUserChatManager.getInstanceFor(connection);

            // Get a MultiUserChat using MultiUserChatManager
            MultiUserChat multiUserChat = multiUserChatManager.getMultiUserChat(roomName+"@conference.localhost");

            try {
                multiUserChat.create(nickName);
               Form form = multiUserChat.getConfigurationForm();
               Form submitForm = form.createAnswerForm();

               List<FormField> formFieldList = submitForm.getFields();
               for (FormField formField : formFieldList) {
                 if(!FormField.Type.hidden.equals(formField.getType()) && formField.getVariable() != null) {
                submitForm.setDefaultAnswer(formField.getVariable());
                } 
               }

             submitForm.setAnswer("muc#roomconfig_persistentroom", true);
             submitForm.setAnswer("muc#roomconfig_publicroom", true);

              multiUserChat.sendConfigurationForm(submitForm);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

加入MUC室的方法:

public void joinMultiUserChatRoom(String userName, String roomName) {
        // Get the MultiUserChatManager
        MultiUserChatManager manager = MultiUserChatManager.getInstanceFor(connection);

        // Create a MultiUserChat using an XMPPConnection for a room
        MultiUserChat multiUserChat = manager.getMultiUserChat(roomName + "@conference.localhost");

        DiscussionHistory history = new DiscussionHistory();
        history.setMaxStanzas(-1);
        try {
            multiUserChat.join(userName, "", history, connection.getPacketReplyTimeout());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

按用户获取已加入会议室的列表:

public List<String> getJoinedGroupByUserName(String userName) {
        // Get the MultiUserChatManager
        MultiUserChatManager manager = MultiUserChatManager.getInstanceFor(connection);
        List<String> joinedRooms = null;
        try {
            // Get the rooms where user3@host.org has joined
             joinedRooms = manager.getJoinedRooms(userName+"@conference.localhost");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return joinedRooms;
    }

当用户加入房间时,我收到此消息:“此房间已被锁定,直到确认配置为止。”

1 个答案:

答案 0 :(得分:2)

发送配置后房间不可用(已确认),创作者必须在

之后加入
 multiUserChat.sendConfigurationForm(submitForm);

所以基本上创作者也必须

multiUserChat.join(username)

(如果您不需要留在室内,请在加入后执行muc.leave()