【问题标题】:Strophe MAM how to display the messages?Strophe MAM如何显示消息?
【发布时间】:2017-08-23 00:36:58
【问题描述】:

我已经设法使用 Strophe MAM 将存档消息放入 RAWInput,并显示最后一条消息(但仅显示最后一条)。如何显示来自 RAWInput 的所有消息?但不只是最后一个?

我如何提取消息来自谁?

我已将消息限制为最后 5 条。

connection.mam.query("test3@macbook-pro.local", {
  "with": "test4@macbook-pro.local","before": '',"max":"5",
  onMessage: function(message) {


            console.log( $(message).text());




  },
  onComplete: function(response) {
            console.log("Got all the messages");

  }
    });

【问题讨论】:

    标签: javascript strophe


    【解决方案1】:

    您可以使用 `strophe.mam.js 插件获取所有消息

    这是我的工作代码:

    // Retrives the messages between two particular users.
    
    var archive = [];
    
    var q = {
        onMessage: function(message) {
            try {
                var id = message.querySelector('result').getAttribute('id');
                var fwd = message.querySelector('forwarded');
                var d = fwd.querySelector('delay').getAttribute('stamp');
                var msg = fwd.querySelector('message');
                var msg_data = {
                    id:id,
                    with: Strophe.getBareJidFromJid(msg.getAttribute('to')),
                    timestamp: (new Date(d)),
                    timestamp_orig: d,
                    from: Strophe.getBareJidFromJid(msg.getAttribute('from')),
                    to: Strophe.getBareJidFromJid(msg.getAttribute('to')),
                    type: msg.getAttribute('type'),
                    body: msg.getAttribute('body'),
                    message: Strophe.getText(msg.getElementsByTagName('body')[0])
                };
                archive.val(archive.val() + msg_data.from + ":" + msg_data.message + "\n" + msg_data.to + ":" + msg_data.message + "\n");
                archive.scrollTop(archive[0].scrollHeight - archive.height());
                console.log('xmpp.history.message',msg_data.message);
            } catch(err){
                if(typeof(err) == 'TypeError'){
                    try {
                        console.log(err.stack)
                    } catch(err2){
                        console.log(err,err2);
                    }
                }
            }
            return true;
        },
        onComplete: function(response) {
            console.log('xmpp.history.end',{query:q, response:response});
        }
    };
    
    $(document).ready(function)(){
    archive = $("#archive-messages");
    archive.val("");
    $("#to-jid").change(function() {
                $("#archive-messages").val("");
                var to = {"with": $(this).val()};
                $.extend(q, to, before, max);
                // It takes around 800ms to auto login. So after this timeout we have to retrieve the messages of particular User.
                setTimeout(function(){
                connection.mam.query(Strophe.getBareJidFromJid(connection.jid), q);
            }, 1000);
            });
    });
    

    【讨论】:

    • 谢谢!!它有效,我只是想知道 Openfire 存档有轻微的延迟,对吗?有什么办法可以减少延迟时间?
    • @John 很高兴听到它有效。如果可行,您可以接受答案,以便其他用户可以轻松解决此问题。而且我还没有使用 Openfire。现在我正在使用 ejabberd 进行归档。它没有任何延迟。与其他 XMPP 服务器相比,Ejabberd 非常好
    • yup 接受了答案,您认为使用 Ejabbered 是否更难?如果它很难设置,离线消息,存档,用 Ejabbered ping?因为我仍然很想切换到 Ejabberd,只是不确定它比 Openfire 好多少。
    • @John 根据我的经验,使用 Ejabberd 非常容易。安装和设置要容易得多。您可以在 ejabberd 中轻松配置所有这些离线消息、存档、ping、mam 和其他一些配置。根据我的建议,使用 ejabberd 并不会让你变得强硬。它比openfire好多了。如果您通过您的邮件 ID,我会将一些 XMPP 服务器之间的比较发送给您。以便您做出自己的选择。
    【解决方案2】:
    connection.mam.query("Your_Id", {
      "with": "partner_id","before": '',"max":'',
      onMessage: function(message) {
    
    console.log("Message from ", $(message).find("forwarded message").attr("from"),
                    ": ", $(message).find("forwarded message body").text());
    
    return true;
    
      },
      onComplete: function(response) {
                console.log("Got all the messages");
    
      }
        });
    
    This will fetch all History for a user. If you want to limit, then provide max value.
    
    Don't download strophe.mam.js(https://github.com/metajack/strophejs-plugins/tree/master/mam) from github. Its not working. Please copy below strophe.mam.js file.
    
    **strophe.mam.js**
    /* XEP-0313: Message Archive Management
     * Copyright (C) 2012 Kim Alvefur
     *
     * This file is MIT/X11 licensed. Please see the
     * LICENSE.txt file in the source package for more information.
     *
     * TODO:
     * Get RSM from the reply
     * Clean remove onMessage handler afterwards
     * queryid?
     *
     */
    
    Strophe.addConnectionPlugin('mam', {
        _c: null,
        _p: [ "with", "start", "end" ],
        init: function (conn) {
            this._c = conn;
            Strophe.addNamespace('MAM', 'urn:xmpp:mam:0');
        },
        query: function (jid, options) {
            var _p = this._p;
            var attr = {
                type:"set",
                id:jid
            };
            var mamAttr = {xmlns: Strophe.NS.MAM};
            var iq = $iq(attr).c("query", mamAttr).c('x',{xmlns:'jabber:x:data'});
    
            iq.c('field',{var:"FORM_TYPE"}).c('value').t("urn:xmpp:mam:0").up().up();
            for (i = 0; i < this._p.length; i++) {
                var pn = _p[i];
                var p = options[pn];
                delete options[pn];
                if (!!p) {
                    var f
                    iq.c('field',{var:pn}).c('value').t(p).up().up();
                }
            }
            iq.up();
    
            var onMessage = options["onMessage"];
            delete options['onMessage'];
            var onComplete = options["onComplete"];
            delete options['onComplete'];
            iq.cnode(new Strophe.RSM(options).toXML());
    
            this._c.addHandler(onMessage, Strophe.NS.MAM, "message", null);
            return this._c.sendIQ(iq, onComplete);
        }
    });
    

    【讨论】:

      【解决方案3】:

      检索所有消息的最简单方法是在onMessage() 函数的末尾包含这一行:

      return true;

      我认为原因是如果一个处理程序没有返回true,它会在第一次调用后被销毁。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-10-04
        • 1970-01-01
        • 2021-06-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-17
        • 1970-01-01
        相关资源
        最近更新 更多