【问题标题】:Smack : Is it possible to get the message composed by the user as he writes it in a chat?Smack :是否有可能在用户在聊天中编写消息时获取他编写的消息?
【发布时间】:2020-05-19 21:57:16
【问题描述】:

我们有一个使用 Smack、XMPP、ejabberd 的聊天应用程序。我想知道是否有可能实现以下目标:

  1. 在 2 个用户之间打开了聊天。
  2. User1 正在输入一些消息。
  3. 用户 2 能够在用户 1 键入的消息时看到他键入的消息。

我做了一些研究,但找不到任何相关的东西。

请告诉我如何实现这个用例。

提前致谢!

【问题讨论】:

    标签: spring xmpp chat ejabberd smack


    【解决方案1】:

    这是不可能的。您可以将文本作为用户类型发送,但每个部分都将作为独立消息发送。如果你想要那个,你可能想要有自己的方式来做到这一点。 XMPP 没办法!

    【讨论】:

      【解决方案2】:

      使用 Smack 很容易,只要用户更改文本,您就可以使用 ChatStateExtension 向其他人发送输入节:

       private void sendTypingStatus(final String toJid) {
           //you must have a valid xmpp connection of course
           if (null == mConnection)
              return;
      
          try {
              Message message = new Message(JidCreate.from(toJid));
              message.addExtension(new ChatStateExtension(ChatState.composing));
      
              message.setType(Message.Type.chat);
              mConnection.sendStanza(message);
      
          } catch (InterruptedException | SmackException.NotConnectedException | XmppStringprepException ex) {
              Log.w(TAG, "sendTypingStatus error", ex);
          }
      }
      

      其他人应该准备好接收该节并正确使用它。最好的选择是使用ChatStatesStanzaListener

      public class ChatStatesStanzaListener implements StanzaListener {
      
      private static final String TAG = ChatStatesStanzaListener.class.getSimpleName();
      
      @Override
      public void processStanza(Stanza packet) {
          Message message = (Message) packet;
          if (message.hasExtension(ChatStateExtension.NAMESPACE)) {
              ChatStateExtension chatStateExtension = (ChatStateExtension) message.getExtension(ChatStateExtension.NAMESPACE);
              ChatState chatState = chatStateExtension.getChatState();
              String fromJid = message.getFrom().asBareJid().toString();
      
              if (message.getType().equals(Message.Type.chat)) {
                  Log.v(TAG, "got chat state " + fromJid + " " + message.getType() + " " + chatState);
       //you got youe information here, call a callback or broadcast an event, whatever
      
              } else if (message.getType().equals(Message.Type.groupchat)) {
                  //out of your question
              }
          }
      }
      

      }

      一旦 xmpp 连接建立,不要忘记将节监听器添加到您的 xmpp 连接中: 1.设置节过滤器:

       // set up a stanzalistener and filter chatstates messages only
              StanzaFilter chatStatesStanzaFilter = stanza -> {
                  // filter for chatstates message only
                  return stanza.hasExtension(ChatStateExtension.NAMESPACE);
              };
      
      1. 在初始化 xmpp 连接后使用它:

        mConnection.addAsyncStanzaListener(new ChatStatesStanzaListener(), chatStatesStanzaFilter);
        

      希望对你有帮助:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-03-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-19
        相关资源
        最近更新 更多