-2

I am trying to create sample java application to implement the MultiUserChat of XMPP. Some how I can able to create user and make it online in openfire. Can any one suggest how to join all the users to the created chatRoom?

Here is my sample code inside the class SampleMultiUserChat Where I invite all the users to join the group but it is not getting joined. What I am missing?

SampleMultiUserChat(){
    oConnectionConfiguration = new ConnectionConfiguration("10.10.1.105",5223);
    createChatRoom();
}

/**
 * @param args
 */
public static void main(String[] args) {
    SampleMultiUserChat oSampleMultiUserChat = new SampleMultiUserChat();

    for(int i = 2; i < 4; i++){
        oSampleMultiUserChat.openXMPPConnection("user"+i);
        oSampleMultiUserChat.createAcceptInvitationListener("user"+i);
        oSampleMultiUserChat.inviteToJoinRoom("user"+i);
    }

    Thread mainThread = Thread.currentThread();
    while(true){
        try {
            mainThread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

private void openXMPPConnection(String user){
    XMPPConnection oXmppConnection = new XMPPConnection(oConnectionConfiguration);
    try {
        oXmppConnection.connect();
        oXmppConnection.login(user, "60474c9c10d7142b7508ce7a50acf414");
        userConnection.put(user, oXmppConnection);
    } catch (XMPPException e) {
        System.out.println("Exception occured in login in user : "+user);
    }
}

private void createChatRoom(){
    XMPPConnection oXmppConnection = new XMPPConnection(oConnectionConfiguration);
    try {
        oXmppConnection.connect();
        oXmppConnection.login("user1", "60474c9c10d7142b7508ce7a50acf414");

        myChattingRoom = new MultiUserChat(oXmppConnection, "mychattingroom@conference.10.10.1.105");

        myChattingRoom.create("roomNickName");
        myChattingRoom.sendConfigurationForm(new Form(Form.TYPE_SUBMIT));
    } catch (XMPPException e) {
        e.printStackTrace();
    }
}

private void inviteToJoinRoom(String user){
    myChattingRoom.invite(user+"@10.10.1.105", "Please join my chatting room");
    System.out.println("sent invitation by "+user);
}

private void sendMessage(String msg){
    try {
        myChattingRoom.sendMessage(msg);
    } catch (XMPPException e) {
        System.out.println("Exception occured while sending msg to chat room"+e);
    }
}

private void createAcceptInvitationListener(String user){
    MultiUserChat.addInvitationListener(userConnection.get(user), new InvitationListener() {

        public void invitationReceived(Connection connection, String room, String inviter,
                String reason, String password, Message msg) {
            try {
                myChattingRoom.join(connection.getUser().substring(0, connection.getUser().indexOf("@")));
            } catch (XMPPException e) {
                e.printStackTrace();
            }
        }
    });
}

Thanks in advance.

parthiban
  • 115
  • 1
  • 1
  • 9
  • Possible duplicate of [Adding participants to XMPP chat rooms](http://stackoverflow.com/questions/14098075/adding-participants-to-xmpp-chat-rooms) – legoscia Jul 07 '16 at 14:39
  • My problem is that even if I invite the user to join the group, the user is not get joined in the group. This is my question. Any idea? @ legoscia – parthiban Jul 08 '16 at 05:28
  • Yes, as described in [this answer](http://stackoverflow.com/a/23953973/113848), there is no way to force a user to join a group: the user receives the invite, and has to decide how/whether to act on it. – legoscia Jul 08 '16 at 09:44
  • Do you control the clients? If yes, I can tell you a solution, otherwise what legoscia said... – MrPk Jul 09 '16 at 19:03
  • what do you mean by controlling the clients? in the invitation received methods I am joining the user in the chat room 'myChattingRoom' but it is not joining. why? @MrPK – parthiban Jul 11 '16 at 06:23

1 Answers1

0

I solved my above problem by creating new instance of MultiUserChat.

Here is my edited method 'createAcceptInvitationListener'

private void createAcceptInvitationListener(String user){
    System.out.println("inside create accept invitation listener");
    final XMPPConnection oXmppConnection = userConnection.get(user);

    MultiUserChat.addInvitationListener(oXmppConnection, new InvitationListener() {

        public void invitationReceived(Connection connection, String room, String inviter,
                String reason, String password, Message msg) {
            System.out.println("inside invitation received method");
            try {
                System.out.println(connection.getUser().substring(0, connection.getUser().indexOf("@")));
                MultiUserChat myChattingRoom = new MultiUserChat(oXmppConnection, "mychattingroom@conference.10.10.1.105");

                myChattingRoom.join(connection.getUser().substring(0, connection.getUser().indexOf("@")));

            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("Exception occured while joining the chat room : "+e);
            }
        }
    });
}

private void reservedRoomsCreation(MultiUserChat myChattingRoom) throws XMPPException{
    Form form = myChattingRoom.getConfigurationForm();
    Form submitForm = form.createAnswerForm();
    for(Iterator fields = form.getFields(); fields.hasNext();){
        FormField formFields = (FormField) fields.next();
        if (!FormField.TYPE_HIDDEN.equals(formFields.getType()) && formFields.getVariable() != null) {
            submitForm.setDefaultAnswer(formFields.getVariable());
        }
    }
    submitForm.setAnswer("muc#roomconfig_persistentroom", true);
    myChattingRoom.sendConfigurationForm(submitForm);
}
parthiban
  • 115
  • 1
  • 1
  • 9