MultipleChatUser XMPP asmack join
I'm new to this OpenFire and asmack, I want the user to have the Multi Users Chatting feature, so I searched around and I found MUC, I implemented this to create a room and send invitations to other users this works, the other user gets the invitation, but another user cannot join the room.
I do this when I receive another user invitation
Here, connection is the connection of that user, and room is the name of the room that we get in the invitation.
MultiUserChat muc3 = new MultiUserChat (connection, room)
muc3.join ("testbot3");
testbot3 is just a random name.
But this is causing the 404 error.
Do I need to join the user before sending the invitation i.e. if the user sending the invitation to B sent before the invitation, then A has to join those default users in the room, and then it is up to B to refuse or just save.
What I am doing is B receives an invitation from A in this InvitingListner from B, I am trying to join the above code.
I have been trying for a long time, I am not sure what is going wrong, someone can give some sample code how to do this, it will be a great help for me.
thank
Here is some more info about my problem
As I go and check Openfire I can see the room created by the user and he added himself as the owner, so I don't think this will be a problem when creating the room.
Maybe this could be a problem when the room gets locked as I read the room when it was not fully created, I think it is a problem with filling out the form when we create the room, I am not filling out the password in the form, this could be a problem ?
Please see the following code below inside the handler. I am calling the "checkInvitation" method, which does the same as above, the code is sent 404 more. Could you please tell me what I am wrong in my code.
Do I need to add an alias to add, or something specific to the user?
public void createChatroom () {
MultiUserChat muc = null;
try {
muc = new MultiUserChat(connection, "myroom@conference.localhost");
muc.create("testbot");
// Get the the room 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();
}
});
}
}
source to share
The 404 error indicates that:
404 error can occur if the room does not exist or is locked
So, make sure your room isn't locked or doesn't exist! Below is the code for how I join the room when there is an 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 configure 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 configuration, I can easily enter the room without a password.
source to share