在openfire中延迟群发消息

Sur*_*wah 10 android openfire smack

我使用Openfire(xmpp)服务器构建了一个聊天应用程序.一对一的人聊天工作正常,消息即时传递.但是当我们在组内发送消息时,第一条消息会被延迟,第二条消息会立即传递.

MultiUserChatManager groupChat =
          MultiUserChatManager.getInstanceFor(connection).getMultiUserChat("group_name");
groupChat.send("Message object");
Run Code Online (Sandbox Code Playgroud)

为什么第一条消息会被延迟?

MUC创作是

 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();
    }
Run Code Online (Sandbox Code Playgroud)

MrP*_*rPk 3

存在一个关于创建的问题以及发送时传播的一种副作用。

我认为您需要第一次加入聊天,因为您之前没有加入,并且第一条消息也会激活服务器上的群聊,因此第一条消息会被延迟,因为您没有完成多用户聊天的创建。

怎么修。

在创建阶段,这部分需要改进:

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());
        }
Run Code Online (Sandbox Code Playgroud)

只需:

boolean createNow
try
{
   if (!mchat.isJoined())
   {
       createNow = mchat.createOrJoin(username);
   }
}
catch (Exception e)
{
  throw new Exception("ERROR!");
}
Run Code Online (Sandbox Code Playgroud)

并在此调用之后:

mchat.sendConfigurationForm(submitForm);
Run Code Online (Sandbox Code Playgroud)

添加:

if (!mchat.isJoined()) {
  mchat.join(username);
}
Run Code Online (Sandbox Code Playgroud)

createOrJoin方法是关于创建加入(如名称所示):要激活聊天,您必须在创建阶段后加入它。

但是,createOrJoin由于对已加入的房间进行双重检查以保持客户端中的会话和服务器上的会话之间的同步,因此可能mchat.join() 会出现意外行为,因此必须在. 显式名称听起来像:mustCreateBeforeOrCanJoinDirectly()