【问题标题】:How to create xmpp group chat and add members using smack 4.1 in android [closed]如何在android中使用smack 4.1创建xmpp群聊和添加成员[关闭]
【发布时间】:2016-12-23 15:34:43
【问题描述】:

在我的应用程序中,我想使用 smack 4.1 创建组并将用户添加到组中。 我能够创建组但无法在该组中添加用户。 我的创建组代码是...

创建组

if (connection != null) {
        String groupName = XmppUtils.buildMUCName(grpName);
        MultiUserChat multiUserChat = managerMuc.getMultiUserChat(groupName);
        if (!multiUserChat.isJoined()) {
            boolean createNow;
            try {
                multiUserChat.createOrJoin(grpName);
                createNow = true;
                if (createNow) {
                    Form form = multiUserChat.getConfigurationForm().createAnswerForm();
                    form.setAnswer("muc#roomconfig_publicroom", true);
                    form.setAnswer("muc#roomconfig_roomname", grpName); //                        form.setAnswer("muc#roomconfig_roomowners", userDate.getUserId().toString() + "@" + Config.ChatValues.SERVICE_NAME);
                    form.setAnswer("muc#roomconfig_persistentroom", true);
                    List<String> cast_values = new ArrayList<String>();
                    cast_values.add("moderator");
                    cast_values.add("participant");
                    cast_values.add("visitor");
                    form.setAnswer("muc#roomconfig_presencebroadcast", cast_values);
                    multiUserChat.sendConfigurationForm(form);
                    multiUserChat.join(userDate.getUserId().toString() + "@" + Config.ChatValues.SERVICE_NAME); //                        multiUserChat.sendConfigurationForm(new Form(DataForm.Type.submit)); //this is to create the room immediately after join.
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            Log.e("createGroup", "=======multiUserChat.isJoined()=====>");
        }
    }

添加用户的代码 这就是我正在做的添加用户

  // Create a MultiUserChat using an XMPPConnection for a room

   String roomName = groupName + "@" + Config.ChatValues.GROUP_SERVICE_NAME;
    MultiUserChat muc2 = managerMuc.getMultiUserChat(roomName);
    // User2 joins the new room
    // The room service will decide the amount of history to send
    // The number of chat room services will decide to accept the historical record
   /* DiscussionHistory history = new DiscussionHistory();
    history.setMaxStanzas(0);*/

    //history.setSince(new Date());
    try {

        for (int i = 0; i < selectedLisrArray.size(); i++) {
            String userId = selectedLisrArray.get(i).userId.toString() + "@" + Config.ChatValues.SERVICE_NAME;
            Log.e("joinGroups", "=========>" + roomName + " users to join  " + userId);

            muc2.invite(msg, userId, "let's  join this room ");
            muc2.sendMessage(userId + " : You have joined the group : " + roomName);
        }

    } catch (SmackException.NotConnectedException e) {
        e.printStackTrace();
    }

【问题讨论】:

  • 你好,你是为了群聊学习和尝试的?
  • 我可以创建组但​​无法在该组中添加用户
  • 然后发布您尝试过的代码,其他人如何理解您的问题以及为什么您无法在聊天中添加用户?
  • 你应该去参加会议并在房间里添加用户。

标签: android xmpp chat smack groupchat


【解决方案1】:

我也做了一点点。只要你发送邀请,然后在 openfire 上安装插件订阅,如果你使用过。更改设置以接受任何请求。

如果您不使用 openfire,那么您只需接受对方的邀请即可。 希望这会对你有所帮助。

MultiUserChat muc = new MultiUserChat(XMPP.getInstance().getConnection(getActivity()), groupname);
                try {
                    muc.create(StringUtils.parseName(XMPP.getInstance().getConnection(getActivity()).getUser()));
                    muc.sendConfigurationForm(new Form(Form.TYPE_SUBMIT));
                } catch (XMPPException e) {
                } catch (NoResponseException e) {
                    e.printStackTrace();
                } catch (SmackException e) {
                    e.printStackTrace();
                }

                for (int i = 0; i < alSelectedContacts.size(); i++) {

                    Log.e("tag", "group chating purpose1 ::" + alSelectedContacts.get(i).get("id"));
                    try {
                        muc.invite((alSelectedContacts.get(i).get("id") + "_user") + "@" + XMPP.HOST,
                                alSelectedContacts.get(i).get("id") + "_user");
                    } catch (NotConnectedException e) {
                        e.printStackTrace();
                    }

                }

                try {
                    muc.sendMessage("New group created");
                } catch (NotConnectedException e1) {
                    e1.printStackTrace();
                } catch (XMPPException e1) {
                    e1.printStackTrace();
                }

在这里获得 Muc 邀请,

MultiUserChat.addInvitationListener(mXmppConnection,
        new InvitationListener() {

            @Override
            public void invitationReceived(Connection connection,
                    String room, String inviter, String reason,
                    String unKnown, Message message) {

                //MultiUserChat.decline(mXmppConnection, room, inviter,
                    //  "Don't bother me right now");
                // MultiUserChat.decline(mXmppConnection, room, inviter,
                // "Don't bother me right now");
                try {
                   muc.join("test-nick-name");
                   Log.e("abc","join room successfully");
                   muc.sendMessage("I joined this room!! Bravo!!");
                } catch (XMPPException e) {
                   e.printStackTrace();
                   Log.e("abc","join room failed!");
                }
            }
        });

【讨论】:

  • 其他用户如何获得此邀请以及他如何接受
  • 邀请作为消息获取,然后只需调用它,muc.join("test-nick-name");
  • 我已经编辑了我的答案,请检查。
  • 我必须在用户登录后添加这个 addInvitationListener ?
  • 是的,请确保您已连接到服务器
猜你喜欢
  • 2017-08-11
  • 2018-06-08
  • 2016-10-25
  • 2015-01-15
  • 2015-10-11
  • 2014-06-05
  • 2016-02-29
  • 2015-01-13
  • 2015-11-09
相关资源
最近更新 更多