如何从openfire服务器使用Java Smack库检索聊天历史记录?

时间:2015-06-21 06:00:27

标签: xmpp openfire smack

在Openfire服务器中安装Open Archive插件后,我可以看到来自openfire管理面板的两个用户之间的聊天对话非常简单,也是基于网络的。现在我想从我使用Smack库的聊天客户端应用程序(用java编写)中检索那些对话或聊天记录。我没有找到任何有用的资源。任何建议都会有所帮助。

5 个答案:

答案 0 :(得分:6)

Smack刚刚实现了MAM功能[XEP 0313]但尚未发布,如果您想使用此功能构建来自source的smack库,您希望在下一版本中获取它,或者您可以使用自定义IQ来获取存档来自服务器的消息。

答案 1 :(得分:4)

这可能是一个迟到的答案,但现在SMACK API支持XEP-0136和XEP-0313,因此下面的代码可以帮助人们登陆此页面。

public MamManager.MamQueryResult getArchivedMessages(String jid, int maxResults) {

        MamManager mamManager = MamManager.getInstanceFor(connection);
        try {
            DataForm form = new DataForm(DataForm.Type.submit);
            FormField field = new FormField(FormField.FORM_TYPE);
            field.setType(FormField.Type.hidden);
            field.addValue(MamElements.NAMESPACE);
            form.addField(field);

            FormField formField = new FormField("with");
            formField.addValue(jid);
            form.addField(formField);

            // "" empty string for before
            RSMSet rsmSet = new RSMSet(maxResults, "", RSMSet.PageDirection.before);
            MamManager.MamQueryResult mamQueryResult = mamManager.page(form, rsmSet);

            return mamQueryResult;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

答案 2 :(得分:2)

您正在寻找的解决方案符合XMPP规范的XEP-0136消息存档,但Smack尚未实现此功能。但您可以使用" custom-stanza"从服务器检索消息历史记录。 SMACK API提供的功能。以下链接描述了如何发送自定义节。 " How retrieve Chat History using Java Smack library from openfire server?"

答案 3 :(得分:1)

最后我得到了答案。存档消息传递功能目前尚未在Smack库中实现。

https://community.igniterealtime.org/message/249993#249993

答案 4 :(得分:0)

Maybe I am late to answer this question, but may be it will be helpful for others.

get function result of getArchivedMessages(jid, maxResults); from https://stackoverflow.com/a/41777582/6771052

public List<ChatMessage> getChatHistoryWithJID(String jid, int maxResults) {
    List<ChatMessage> chatMessageList = new ArrayList<>();
    MamManager.MamQueryResult mamQueryResult = getArchivedMessages(jid, maxResults);
    String userSendTo = XmppUtils.parseNameFromJID(jid);

    try {
        if (mamQueryResult != null && userSendTo != null) {
            for (Forwarded forwarded : mamQueryResult.forwardedMessages) {
                if (forwarded.getForwardedStanza() instanceof Message) {
                    Message msg = (Message) forwarded.getForwardedStanza();
                    Log.d(TAG, "onCreate: " + msg.toString());
                    Log.d(TAG, "processStanza: " + msg.getFrom() + " Say:" + msg.getBody() + " String length:" + (msg.getBody() != null ? msg.getBody().length() : ""));
                    ChatMessage chatMessage;
                    if (XmppUtils.parseNameFromJID(msg.getFrom().toString()).equalsIgnoreCase(userSendTo)) {
                        chatMessage = new ChatMessage(msg.getBody(), forwarded.getDelayInformation().getStamp().getTime(), ChatMessage.Type.RECEIVED);
                    } else {
                        chatMessage = new ChatMessage(msg.getBody(), forwarded.getDelayInformation().getStamp().getTime(), ChatMessage.Type.SENT);
                    }
                    chatMessageList.add(chatMessage);

                }
            }
        } else {
            return chatMessageList;
        }

        return chatMessageList;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return chatMessageList;
}

Now the Query will become like:

<iq  id='ri7F7-270' type='set'>
<query  xmlns='urn:xmpp:mam:1' queryid='afd9c922-21cb-437e-b5c4-3a5bf9994e40'>
    <x  xmlns='jabber:x:data' type='submit'>
        <field  var='FORM_TYPE' type='hidden'>
            <value>urn:xmpp:mam:1</value>
        </field>
        <field  var='with'>
            <value>vishal@jabberid</value>
        </field>
    </x>
    <set  xmlns='http://jabber.org/protocol/rsm'>
        <before>
        </before>
        <max>100</max>
    </set>
</query>

And the response will be like:

<message to="vishal@jabberid">
<result xmlns="urn:xmpp:mam:1"  queryid="afd9c922-21cb-437e-b5c4-3a5bf9994e40" id="992">
<forwarded xmlns="urn:xmpp:forward:0">
  <delay xmlns="urn:xmpp:delay" stamp="2019-04-05T06:38:40.612Z"/>
  <message xmlns="jabber:client" to="vishal@jabberid" id="h58k4-104" type="chat" from="vishal@jabberid">
    <body>Hi</body>
  </message>
</forwarded>
</result>
</message>

And more you can be read from this link https://xmpp.org/extensions/xep-0313.html