如何在smack 4.1 beta2中创建持久的muc空间

时间:2015-02-09 12:46:41

标签: xmpp ejabberd smack multiuserchat

从asmack迁移到smack 4.1 beta2。 创建的muc房间不再持久。

MultiUserChatManager mucm=MultiUserChatManager.getInstanceFor(connection);
muc=mucm.getMultiUserChat(groupid+"@conference.localhost");
DiscussionHistory histroy=new DiscussionHistory();
histroy.setMaxStanzas(10);
muc.createOrJoin(username,null,histroy,SmackConfiguration.getDefaultPacketReplyTimeout());
muc.nextMessage();

使用gajim创建时,房间是持久的。

编辑:这是我们之前使用的代码。默认情况下,聊天室是持久的,

muc = new MultiUserChat(connection, groupid+"@conference.localhost");

if(!muc.isJoined())
{
DiscussionHistory histroy=new DiscussionHistory();
histroy.setMaxStanzas(10);
muc.join(username,null,histroy,SmackConfiguration.getDefaultPacketReplyTimeout());
muc.nextMessage(0);
}

4 个答案:

答案 0 :(得分:5)

您需要提交这样的表单以制作持久群组:

private void setConfig(MultiUserChat multiUserChat) {
    try {
        Form form = multiUserChat.getConfigurationForm();
        Form submitForm = form.createAnswerForm();
        for (Iterator<FormField> fields = submitForm.getFields(); fields.hasNext();) {
            FormField field = (FormField) fields.next();
            if (!FormField.TYPE_HIDDEN.equals(field.getType()) && field.getVariable() != null) {
                submitForm.setDefaultAnswer(field.getVariable());
            }
        }
        submitForm.setAnswer("muc#roomconfig_publicroom", true);
        submitForm.setAnswer("muc#roomconfig_persistentroom", true);
        multiUserChat.sendConfigurationForm(submitForm);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

答案 1 :(得分:4)

您需要在创建房间时在MUC配置中将muc#roomconfig_persistentroom设置为true

MultiuserChat muc = manager.getMultiUserChat("myroom@muc.example.org");
muc.create("myNick");
// room is now created by locked
Form form = muc.getConfigurationForm();
Form answerForm = form.createAnswerForm();
answerForm.setAnswer("muc#roomconfig_persistentroom", true);
muc.sendConfigurationForm(answerForm);
// sending the configuration form unlocks the room

请注意,并非所有XMPP MUC服务都支持持久性会议室。有关更多信息,请参阅:

答案 2 :(得分:1)

在smack 4.1.1中,@ saurabh dixit给出的答案会引发异常。请在点燃网站Correct implementation for persistent rooms smack 4.1.1

上查看此主题

答案 3 :(得分:0)

        multiUserChatManager = MultiUserChatManager.getInstanceFor(connection);

        multiUserChat = multiUserChatManager.getMultiUserChat(JidCreate.entityBareFrom(roomJID));

        multiUserChat.create(Resourcepart.from(nickname));

        Form form = multiUserChat.getConfigurationForm();
        Form submitForm = form.createAnswerForm();
        submitForm.getField("muc#roomconfig_enablelogging").addValue("1");
        submitForm.getField("x-muc#roomconfig_reservednick").addValue("0");
        submitForm.getField("x-muc#roomconfig_canchangenick").addValue("0");
        submitForm.getField("x-muc#roomconfig_registration").addValue("0");
        submitForm.getField("muc#roomconfig_passwordprotectedroom").addValue("0");
        submitForm.getField("muc#roomconfig_roomname").addValue(roomName);
        submitForm.getField("muc#roomconfig_whois").addValue("participants");
        submitForm.getField("muc#roomconfig_membersonly").addValue("1");
        submitForm.getField("muc#roomconfig_persistentroom").addValue("1");
        multiUserChat.sendConfigurationForm(submitForm);

这是从中发送房间配置和配置房间的方法。 有关更多详细信息,请参阅问题。 How to send room configuration form and create persistce rooms from android using smack 4.3.4