代码之家  ›  专栏  ›  技术社区  ›  Sangeetha

创建MultiUserChat时出现“$XMPPErrorException:XMPPEror:forbidden-auth”错误

  •  2
  • Sangeetha  · 技术社区  · 9 年前

    我已经用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));
    

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

    2 回复  |  直到 9 年前
        1
  •  1
  •   MrPk    9 年前

    尝试以下代码:

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

    现在,如果一切正常,您将以用户名加入房间,用户名也将是房间的所有者。

    您可以通过以下方式以编程方式检查MUC的所有者

    muc.getOwners()  //List<Affiliate>, for each Affialiate you'll have to affiliate.getJid().toString()
    

    您可以通过以下代码行邀请人员:

    muc.invite(user, "Invite");
    

    然后,如果你想“永远”看到他们,

    muc.grantMembership(user);
    

    这样你就可以看到

    muc.getMembers();
    

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

    并非所有占用者都可以拥有角色,并非所有关联方都自动成为占用者。

    此外,你不能确定某个代销商是否加入过群聊。

    Flux是这样的:

    用户创建Muc 1 (可选)用户1向任何用户发出的Muc邀请(例如:用户2、用户4) (可选)用户1向其所需的任何现有用户分配多个关联方(例如:用户3、用户4)

    在线时,User2和User4将收到接受/拒绝邀请 User3和User4不会收到任何消息,但它们将在MUC中扮演角色。

    用户2、用户3、用户4需要注册IQProviders以获取IQ Stanzas,然后每个MUC的列表者接收邀请,另一个MUC接收消息(和/或其他事件)。

        2
  •  0
  •   shekhar pande    5 年前

    对于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)的方式。