我使用Openfire(xmpp)服务器构建了一个聊天应用程序。一对一的人聊天工作正常,消息即时传递。但是当我们在组内发送消息时,第一条消息会被延迟,第二条消息会立即传递。
MultiUserChatManager groupChat =
MultiUserChatManager.getInstanceFor(connection).getMultiUserChat("group_name");
groupChat.send("Message object");
为什么第一条消息会延迟?
MUC Creation
MultiUserChatManager mchatManager = MultiUserChatManager.getInstanceFor(xmpptcpConnection);
MultiUserChat mchat = mchatManager.getMultiUserChat(group);
if (!mchat.isJoined()) {
Log.d("CONNECT", "Joining room !! " + group + " and username " + username);
boolean createNow = false;
try {
mchat.createOrJoin(username);
createNow = true;
} catch (Exception e) {
Log.d("CONNECT", "Error while creating the room " + group + e.getMessage());
}
if (createNow) {
Form form = mchat.getConfigurationForm();
Form submitForm = form.createAnswerForm();
List<FormField> formFieldList = submitForm.getFields();
for (FormField formField : formFieldList) {
if(!FormField.Type.hidden.equals(formField.getType()) && formField.getVariable() != null) {
submitForm.setDefaultAnswer(formField.getVariable());
}
}
submitForm.setAnswer("muc#roomconfig_persistentroom", true);
submitForm.setAnswer("muc#roomconfig_publicroom", true);
mchat.sendConfigurationForm(submitForm);
//mchat.sendConfigurationForm(
// new Form(DataForm.Type.submit)); //this is to create the room immediately after join.
}
}
Log.d("CONNECT", "Room created!!");
return true;
} catch (SmackException e) {
e.printStackTrace();
} catch (XMPPException.XMPPErrorException e) {
e.printStackTrace();
}
答案 0 :(得分:3)
关于创建和在发送时传播的一种副作用存在问题。
我认为你需要第一次加入聊天,因为你之前没有,第一条消息也激活了服务器上的群聊,所以第一条消息由于你没有最终确定多用户聊天而被推迟。 / p>
如何解决。
在创作阶段,必须改进这部分:
if (!mchat.isJoined()) {
Log.d("CONNECT", "Joining room !! " + group + " and username " + username);
boolean createNow = false;
try {
mchat.createOrJoin(username);
createNow = true;
} catch (Exception e) {
Log.d("CONNECT", "Error while creating the room " + group + e.getMessage());
}
只有:
boolean createNow
try
{
if (!mchat.isJoined())
{
createNow = mchat.createOrJoin(username);
}
}
catch (Exception e)
{
throw new Exception("ERROR!");
}
在此次调用之后:
mchat.sendConfigurationForm(submitForm);
添加:
if (!mchat.isJoined()) {
mchat.join(username);
}
creationOrJoin 方法是关于创建 OR 加入(如名称所示):要激活聊天,您必须在创建阶段后加入聊天。
但createOrJoin
可能会出现意外行为,原因是对已加入的会议室进行了双重检查,以便在客户端会话和服务器会话之间保持同步,因此必须在}之后调用<{1}} STRONG>。
显式名称听起来像: mustCreateBeforeOrCanJoinDirectly()