3
votes

I am new to this OpenFire and asmack, i want the user to have a functionality of Multi Users Chatting so i searched around and i found MUC i have implemented this for creating a room and sending invitation to other users these works, other user receives the invitation but the other user is not able to join the room.

I am doing this on other user invitation receiving

Here connection is the connection of this user and room is the room name that we getting in invitation.

MultiUserChat muc3 = new MultiUserChat(connection,room);

muc3.join("testbot3");

testbot3 is just some random name.

But this throws 404 error.

Do i need to join the user before sending the invitation i.e if A user sending invitation to B , before invitation sent do A needs to join these users by default to room and then it depends on B to decline or just keep quite.

What i am doing is B receives invitation from A in that InvitationListner of B i am trying to join with the above code.

I have been trying for long now i am not sure what is going wrong, some one can give a sample code of how to do this it would be great help for me.

Thanks

Here is more information on my issue

As i go and check on Openfire i can see the room created by the user and he has been added himself as an owner so i dont think so it would be an issue with room getting created.

May be this can be an issue with room getting locked, as i have read through the room is locked when the room is not completely created , i guess this is an issue with form filling when we create the room, i am not filling in the password in the form can this be an issue ?

Please see the following code below inside the handler i am calling a method "checkInvitation" which does the same as above code posted still i get 404. Can you please tell me what i wrong in my code.

Do the nickname that needs to be added can be anything or it needs to something user specific ?

public void createChatroom(){

    MultiUserChat muc = null;

    try {
      muc = new MultiUserChat(connection, "[email protected]");
      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();
            }
        });
    }
}
3
Hi @Doniv I was able to send messages to a user in a room and the processPacket method is called everytime when i send a message but i am not recieving any message in processPacket method is there any issue or we need to set any other listeners ???KK_07k11A0585

3 Answers

9
votes

The 404 error indicates that:

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

So, ensure that your room is not locked or existed! The code below is how I join the room when there's an in-comming invitation:

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!");
                    }
                }
            });
}

Hope this helps your error!

Edit:this is how I config the room:

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

With this cofiguration, I can join a room easily without password.

0
votes

You may use the code snippet to join the chat:

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

Invite a friend:

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