如何在Smack 4.1 beta2中创建持久的MUC房间

3

已从 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);
}

1
你之前是如何创建持久化房间的?我认为你需要使用 MultiUserChat.create 发送正确的数据表单来创建一个持久化房间。 - Flow
你好@flow,请检查已编辑的问题。 - Vignesh
4个回答

5
你需要在创建房间时,在MUC配置中设置muc#roomconfig_persistentroomtrue
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服务都支持持久化房间。更多信息请参见:

1
我认为 answerForm.setAnswer("muc#roomconfig_persistentroom", "true"); 应该改为 answerForm.setAnswer("muc#roomconfig_persistentroom", true);,将字符串改为布尔值。 - Arun Badole
@flow 应该在"muc#roomconfig_persistentroom"的位置上放置什么? - Sakhawat Hossain

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

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);

这是如何发送房间配置并配置房间的方法。 更多细节请参见问题。 如何使用Smack 4.3.4从Android发送房间配置表单并创建持久化房间

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接