3

我是这个 OpenFire 和 asmack 的新手,我希望用户具有多用户聊天的功能,所以我四处搜索,我发现 MUC 我已经实现了这个用于创建房间并向其他用户发送邀请这些作品,其他用户收到邀请,但其他用户无法加入房间。

我在收到其他用户邀请时这样做

这里的连接是这个用户的连接,房间是我们在邀请中得到的房间名称。

MultiUserChat muc3 = new MultiUserChat(connection,room);

muc3.join("testbot3");

testbot3 只是一些随机名称。

但这会引发 404 错误。

我是否需要在发送邀请之前加入用户,即如果 A 用户向 B 发送邀请,在发送邀请之前 A 需要默认加入这些用户到房间,然后取决于 B 拒绝或只是保持安静。

我正在做的是 B 在我试图加入上述代码的 B 的 InvitationListner 中收到来自 A 的邀请。

我已经尝试了很长时间,现在我不确定出了什么问题,有人可以提供如何执行此操作的示例代码,这对我有很大帮助。

谢谢

这是有关我的问题的更多信息

当我去检查 Openfire 时,我可以看到用户创建的房间,并且他自己被添加为所有者,所以我认为创建房间不会有问题。

可能这可能是房间被锁定的问题,因为我已经阅读了房间没有完全创建时房间被锁定,我想这是我们创建房间时填写表格的问题,我没有填写表单中的密码这可能是个问题吗?

请在处理程序中查看下面的代码,我正在调用一个方法“checkInvitation”,它与上面发布的代码相同,但我得到 404。你能告诉我我的代码有什么问题吗?

需要添加的昵称可以是任何东西还是需要用户特定的东西?

公共无效创建聊天室(){

    MultiUserChat muc = null;

    try {
      muc = new MultiUserChat(connection, "myroom@conference.localhost");
      muc.create("testbot");

      // Get the the room's configuration form
      Form form = muc.getConfigurationForm();
      // Create a new form to submit based on the original form
      Form submitForm = form.createAnswerForm();
      // Add default answers to the form to submit
      for (Iterator fields = form.getFields(); fields.hasNext();) {
          FormField field = (FormField) fields.next();
          if (!FormField.TYPE_HIDDEN.equals(field.getType()) && field.getVariable() != null) {
              // Sets the default value as the answer
              submitForm.setDefaultAnswer(field.getVariable());
          }
      }
      // Sets the new owner of the room
      List owners = new ArrayList();
      owners.add("admin@localhost");
      submitForm.setAnswer("muc#roomconfig_roomowners", owners);
      // Send the completed form (with default values) to the server to configure the room
      muc.sendConfigurationForm(submitForm);
      muc.join("d");

      muc.invite("b@localhost", "Meet me in this excellent room");

      muc.addInvitationRejectionListener(new InvitationRejectionListener() {
          public void invitationDeclined(String invitee, String reason) {
              // Do whatever you need here...
              System.out.println("Initee "+invitee+" reason"+reason);
          }
      });

    } catch (XMPPException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

    public void setConnection(XMPPConnection connection) {
    this.connection = connection;
    if (connection != null) {
        // Add a packet listener to get messages sent to us
        PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
        connection.addPacketListener(new PacketListener() {
            public void processPacket(Packet packet) {
                Message message = (Message) packet;
                if (message.getBody() != null) {
                    String fromName = StringUtils.parseBareAddress(message
                            .getFrom());
                    Log.i("XMPPClient", "Got text [" + message.getBody()
                            + "] from [" + fromName + "]");
                    messages.add(fromName + ":");
                    messages.add(message.getBody());
                    // Add the incoming message to the list view
                    mHandler.post(new Runnable() {
                        public void run() {
                            setListAdapter();
                            checkInvitation();
                        }
                    });
                }
            }
        }, filter);


        mHandler.post(new Runnable() {
            public void run() {
                checkInvitation();
            }
        });
    }
}
4

3 回答 3

9

404 错误表示:

404 error can occur if the room does not exist or is locked

因此,请确保您的房间没有被锁定或存在!下面的代码是我在收到邀请时加入房间的方式:

private void setChatRoomInvitationListener() {
    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!");
                    }
                }
            });
}

希望这有助于您的错误!

编辑:这就是我配置房间的方式:

 /*
         * Create room
         */
        muc.create(roomName);

        // muc.sendConfigurationForm(new Form(Form.TYPE_SUBMIT));
        Form form = muc.getConfigurationForm();
        Form submitForm = form.createAnswerForm();

        for (Iterator fields = form.getFields(); fields.hasNext();) {
            FormField field = (FormField) fields.next();
            if (!FormField.TYPE_HIDDEN.equals(field.getType())
                    && field.getVariable() != null) {
                show("field: " + field.getVariable());
                // Sets the default value as the answer
                submitForm.setDefaultAnswer(field.getVariable());
            }
        }

        List<String> owners = new ArrayList<String>();
        owners.add(DataConfig.USERNAME + "@" + DataConfig.SERVICE);
        submitForm.setAnswer("muc#roomconfig_roomowners", owners);
        submitForm.setAnswer("muc#roomconfig_roomname", roomName);
        submitForm.setAnswer("muc#roomconfig_persistentroom", true);

        muc.sendConfigurationForm(submitForm);
        // submitForm.
        show("created room!");
        muc.addMessageListener(new PacketListener() {
            @Override
            public void processPacket(Packet packet) {
                show(packet.toXML());
                Message mess = (Message) packet;
                showMessageToUI(mess.getFrom() + ": " + mess.getBody());
            }
        });

通过这种配置,我可以轻松加入房间而无需密码。

于 2013-01-23T10:52:49.840 回答
0

您可以使用代码片段加入聊天:

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

        // Create a MultiUserChat using an XMPPConnection for a room
        MultiUserChat multiUserChat = manager.getMultiUserChat(roomName + "@conference.localhost");

        DiscussionHistory history = new DiscussionHistory();
        history.setMaxStanzas(-1);
        try {
            multiUserChat.join(userName, "", history, connection.getPacketReplyTimeout());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
于 2018-06-27T12:32:34.370 回答
0

邀请朋友:

 /**
     * Invites another user to this room.
     *
     * @param userAddress the address of the user to invite to the room.(one
     *   may also invite users not on their contact list).
     * @param reason a reason, subject, or welcome message that would tell
     *   the the user why they are being invited.
     */
    public void invite(String userAddress, String reason)
    {
        multiUserChat.invite(userAddress, reason);
    }
于 2018-06-29T06:19:15.533 回答