XMPP群聊java - 在邀请监听器内加入房间的用户的逻辑不起作用
Posted
技术标签:
【中文标题】XMPP群聊java - 在邀请监听器内加入房间的用户的逻辑不起作用【英文标题】:XMPP Group chat java - logic to join users in room inside invitation listener is not working 【发布时间】:2016-07-07 13:30:01 【问题描述】:我正在尝试创建示例 java 应用程序来实现 XMPP 的 MultiUserChat。我如何能够创建用户并在 openfire 中使其在线。谁能建议如何将所有用户加入创建的聊天室?
这是我在 SampleMultiUserChat 类中的示例代码,我邀请所有用户加入该组,但它没有加入。我错过了什么?
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();
);
提前致谢。
【问题讨论】:
Adding participants to XMPP chat rooms的可能重复 我的问题是,即使我邀请用户加入群组,用户也不会加入群组。这是我的问题。任何的想法? @legoscia 是的,正如this answer 中所述,没有办法强制用户加入群组:用户收到邀请后,必须决定如何/是否采取行动。跨度> 你控制客户吗?如果是,我可以告诉你一个解决方案,否则 legoscia 所说的...... 控制客户端是什么意思?在收到邀请的方法中,我加入了聊天室“myChattingRoom”中的用户,但它没有加入。为什么? @MrPK 【参考方案1】:我通过创建 MultiUserChat 的新实例解决了上述问题。
这是我编辑的方法'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);
【讨论】:
以上是关于XMPP群聊java - 在邀请监听器内加入房间的用户的逻辑不起作用的主要内容,如果未能解决你的问题,请参考以下文章