如何在stack 4.1 beta 2中创建持久的muc空间



从asmack迁移到了stack4.1测试版2。创建的多个房间不再持久。

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

您需要提交这样的表单来创建持久组:

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

创建房间时,需要在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服务都支持持久房间。有关更多信息,请参阅:

  • https://www.igniterealtime.org/builds/smack/dailybuilds/javadoc/org/jivesoftware/smackx/muc/MultiUserChat.html#create(java.lang.String)
  • https://www.igniterealtime.org/builds/smack/dailybuilds/documentation/extensions/muc.html
  • http://xmpp.org/extensions/xep-0045.html#createroom

在第4.1.1小节中,@saurabh dixit给出的答案抛出了一个异常。请在ignite网站上查看此线程。持久性房间的正确实现,请点击4.1.1

        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创建持久房间

最新更新