【问题标题】:MultipleChatUser XMPP asmack joinMultipleChatUser XMPP asmack 加入
【发布时间】:2013-01-06 16:20:59
【问题描述】:

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

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

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

MultiUserChat muc3 = new MultiUserChat(connection,room);

muc3.join("testbot3");

testbot3 只是一些随机名称。

但这会引发 404 错误。

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

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

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

谢谢

这里是关于我的问题的更多信息

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

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

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

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

public void createChatroom(){

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

【问题讨论】:

  • 嗨@Doniv我能够向房间中的用户发送消息,并且每次发送消息时都会调用 processPacket 方法,但我没有在 processPacket 方法中收到任何消息是否有任何问题或我们需要设置任何其他侦听器???

标签: java xmpp openfire asmack


【解决方案1】:

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

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

【讨论】:

  • 当我去查看 Openfire 时,我可以看到用户创建的房间,并且他已被添加为所有者,所以我认为创建时不会有问题。
  • 您是否曾尝试使用不是房间所有者的用户帐户加入房间?
  • 嗨@Kingfisher 我能够向房间中的用户发送消息,并且每次发送消息时都会调用 processPacket 方法,但我在 processPacket 方法中没有收到任何消息是否有任何问题或我们需要设置任何其他侦听器???
  • @KK_07k11A0585 我认为您应该在您的XmppConnection 对象中注册一个侦听器:xmppConnection.addPacketListener(packetListener, null); 并在packetListener 中您可以检查收到的数据包的类型,如下所示:if (packet instanceof org.jivesoftware.smack.packet.Message &amp;&amp; ((org.jivesoftware.smack.packet.Message) packet) .getType().equals(Type.groupchat)) .不要注册MUC,只需注册你的connection
  • @KK_07k11A0585:您应该仔细阅读此链接:xmpp.org/extensions/xep-0045.html#registrar-formtype-owner。配置房间时,有一个字段:` `。您必须对其进行配置才能接收所有历史消息! P/s:下次请再问一个问题!
【解决方案2】:

您可以使用代码 sn-p 加入聊天:

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

【讨论】:

    【解决方案3】:

    邀请朋友:

     /**
         * 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);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-04
      • 1970-01-01
      • 2011-11-15
      • 2014-04-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多