在XMPP中添加成员到MUC(SMACK)

时间:2016-06-23 12:37:06

标签: java xmpp openfire smack multiuserchat

我正在尝试使用XMPP(smack)创建多用户聊天。创建房间后,当我尝试加入聊天室时, ofmucmember 中没有加入成员的条目。

创建房间代码如下:

public void createMultiUserChatRoom(String roomName, String nickName) {

        MultiUserChatManager multiUserChatManager = MultiUserChatManager.getInstanceFor(connection);

        MultiUserChat multiUserChat = multiUserChatManager.getMultiUserChat(roomName+"@conference.localhost");

        try {
            multiUserChat.create(nickName);

            Form form = multiUserChat.getConfigurationForm();
            Form submitForm = form.createAnswerForm();

            List<FormField> formFieldList = submitForm.getFields();
            for (FormField formField : formFieldList) {
                if(!FormField.Type.hidden.equals(formField.getType()) && formField.getVariable() != null) {
                    submitForm.setDefaultAnswer(formField.getVariable());
                }
            }

            submitForm.setAnswer("muc#roomconfig_persistentroom", true);
            submitForm.setAnswer("muc#roomconfig_publicroom", true);
            submitForm.setAnswer("muc#roomconfig_enablelogging", true);
            submitForm.setAnswer("x-muc#roomconfig_reservednick", false);
            submitForm.setAnswer("x-muc#roomconfig_canchangenick", false);
            submitForm.setAnswer("x-muc#roomconfig_registration", false);
            submitForm.setAnswer("muc#roomconfig_passwordprotectedroom", false);
            submitForm.setAnswer("muc#roomconfig_roomname", roomName);
             submitForm.setAnswer("muc#roomconfig_whois", Arrays.asList("none"));
            multiUserChat.sendConfigurationForm(submitForm);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

加入创建的房间的代码如下:

    public void joinMultiUserChatRoom(String userName, String roomName) {
            MultiUserChatManager manager = MultiUserChatManager.getInstanceFor(connection);

            MultiUserChat multiUserChat = manager.getMultiUserChat(roomName + "@conference.localhost");
            DiscussionHistory history = new DiscussionHistory();
            history.setMaxStanzas(0);
            try {
                multiUserChat.join(userName);
                multiUserChat.sendMessage(userName +" : You have joined the group : " + roomName);

                Presence presence = multiUserChat.getOccupantPresence(roomName + "@conference.localhost/" + userName);
                presence.setMode(Presence.Mode.available);
                connection.sendStanza(presence);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

来自服务器的响应:

<message to="admin@localhost/Smack" id="h7axM-14" type="groupchat" from="team6@conference.localhost/roy"><body>roy : You have joined the group : team6</body><x xmlns="jabber:x:delay" stamp="20160623T12:15:50" from="team6@conference.localhost/roy"/></message>
presence :<presence to='admin@localhost/Smack' id='WR9Dy-12'><x xmlns='http://jabber.org/protocol/muc#user'><item affiliation='owner' jid='admin@localhost/Smack' role='moderator'></item></x><c xmlns='http://jabber.org/protocol/caps' hash='sha-1' node='http://www.igniterealtime.org/projects/smack' ver='NfJ3flI83zSdUDzCEICtbypursw='/></presence>

我没有收到任何错误。谁能告诉我我在哪里错了?

2 个答案:

答案 0 :(得分:1)

填写配置表单时,需要填写以下部分:

<field
      label='Room Admins'
      type='jid-multi'
      var='muc#roomconfig_roomadmins'>
    <value>wiccarocks@shakespeare.lit</value>
    <value>hecate@shakespeare.lit</value>
  </field>

<field
          label='Room Owners'
          type='jid-multi'
          var='muc#roomconfig_roomowners'/>

管理员和所有者保存在 ofMucAffiliation 表格中,如果配置已更新且未提及现有管理员或所有者,则服务器会认为其所属关联已更改为成员,因此将条目移动到 ofMucMember 表。

答案 1 :(得分:0)

你只是因为加入一个房间而没有得到会员。加入会议室后,您是XEP-0045术语中的参与者