Getting "$XMPPErrorException: XMPPError: forbidden - auth" error while creating MultiUserChat

Getting "$XMPPErrorException: XMPPError: forbidden - auth" error while creating MultiUserChat

我已经使用 Smack Api(4.1.4) 为 XMPP 成功创建了登录连接。现在我正在尝试使用

创建 MultiUserChat
    try {
        String myMUCName = "TestGroup";
        String myMUCService = "conference.(my local ip)";
        String myMUCfullName = myMUCName + "@" + myMUCService;
        String userName =  "Test5";

        MultiUserChatManager manager = MultiUserChatManager.getInstanceFor(connection);
        MultiUserChat muc = manager.getMultiUserChat(myMUCfullName);
        muc.create(userName);

        Log.d(LOCAL_TAG, "createGroupChat  -- Group CEATED Successfully ");
        Form form = muc.getConfigurationForm();
        Form submitForm = form.createAnswerForm();

        List<FormField> fields = form.getFields();
        Log.d(LOCAL_TAG, "createGroupChat  -- fields.size(): "+fields.size());
        for (int i = 0; i < fields.size(); i++) {
            FormField field = (FormField) fields.get(i);
            if (!FormField.Type.hidden.equals(field.getType()) && field.getVariable() != null) {
                submitForm.setDefaultAnswer(field.getVariable());
            }
        }

        List owners = new ArrayList();
        owners.add(userName); //Own user
        owners.add("Test7"); //Another user

        submitForm.setAnswer("muc#roomconfig_roomowners", owners);
        submitForm.setAnswer("muc#roomconfig_publicroom", true);
        submitForm.setAnswer("muc#roomconfig_persistentroom", true);
        muc.sendConfigurationForm(new Form(DataForm.Type.submit));
        //muc.sendConfigurationForm(submitForm);
    Log.d(LOCAL_TAG, "createGroupChat  -- Sent Configuration");
        muc.join(TestGroup);
        Log.d(LOCAL_TAG, "createGroupChat  -- Group Joined Successfully -- owners.size(): "+owners.size());

但是在创建群组时出现异常

    "org.jivesoftware.smack.XMPPException$XMPPErrorException: XMPPError: forbidden - auth". 

希望如此,这个异常发生在代码

   muc.sendConfigurationForm(submitForm);

出于这个原因,对此进行了评论。因为我没有在那个代码之后得到日志。为了解决这个问题,我将该代码更改为

   muc.sendConfigurationForm(new Form(DataForm.Type.submit));

它修复了该异常并为我创建了一个组,因为我可以看到打印的日志并且可以在明火中看到我的组。但我确实知道我为该组选择的用户是如何通过这样做添加的,因为所有者列表(或提交表单)不包含在任何地方。我不知道这里面发生了什么,我不确定我做的对不对。请建议我如何进行。提前致谢。

试试这个代码:

Form form = muc.getConfigurationForm().createAnswerForm();


        // Create a new form to submit based on the original form
        form.setAnswer("muc#roomconfig_passwordprotectedroom", false);
        form.setAnswer("muc#roomconfig_roomname",myMUCName);
        form.setAnswer("muc#roomconfig_persistentroom", true);
        form.setAnswer("muc#roomconfig_changesubject", true);
        form.setAnswer("muc#roomconfig_publicroom",true);
        form.setAnswer("muc#roomconfig_allowinvites",true);
        form.setAnswer("muc#roomconfig_membersonly",false);
        form.setAnswer("muc#roomconfig_moderatedroom",false);

        // Sets the new owner of the room
        List<String> owners = new ArrayList<String>();

        //Be carefull: if members does not exists, it brokes!

        owners.add(userName +"@"+"(my local ip or server name placeholder)");
        form.setAnswer("muc#roomconfig_roomowners", owners);

        // Send the completed form
        muc.sendConfigurationForm(form);
        System.out.println("MUC is now registered");

        muc.join(userName );

现在,如果一切正常,您将以 userName 的身份加入房间,而 userName 也将是所有者。

您可以通过

以编程方式检查 MUC 的所有者
muc.getOwners()  //List<Affiliate>, for each Affialiate you'll have to affiliate.getJid().toString()

您可以通过这行代码邀请他人:

muc.invite(user, "Invite");

然后,如果你想看他们 "forever",

muc.grantMembership(user);

这样您就可以看到

的会员资格
muc.getMembers();

请注意: 会员:在 MUC 中具有定义角色(Onwer、Admin、Member、Outcast)的用户 占用者:MUC 中的在线用户

并非所有占用者都可以担任角色,并非所有附属机构都自动成为占用者。

此外,您无法确定会员是否曾加入群聊。

Flux 是这样的:

用户 1 创建的 Muc (可选)用户 1 对他想要的任何用户的 Muc 邀请(例如:用户 2、用户 4) (可选)用户 1 将 Muc 关联分配给他想要的任何现有用户(例如:用户 3、用户 4)

用户 2 和用户 4 在线时将收到 accept/reject 的邀请 User3 和 User4 不会收到任何东西,但他们将在 MUC 中发挥作用。

用户 2、用户 3、用户 4 需要注册 IQProvider 以获得 IQ 节,然后每个 MUC 的监听器接收邀请,另一个接收消息(和/或其他事件)。

对于 SMACK 4.3.4 及更高版本。

        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_publicroom").addValue("1");
        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);

这是发送房间配置和创建房间 (MUC) 的方法。