javascript - Strophe MAM how to display the messages? - Stack Overflow

I have managed to use Strophe MAM to get the archived messages into the RAWInput, and display the last

I have managed to use Strophe MAM to get the archived messages into the RAWInput, and display the last message(but only the last one). How do i display all the messages from the RAWInput?? But not just the last one?

And how do i extract who the message is from?

I have limited the messages to the last 5.

connection.mam.query("[email protected]", {
  "with": "[email protected]","before": '',"max":"5",
  onMessage: function(message) {


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




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

  }
    });

I have managed to use Strophe MAM to get the archived messages into the RAWInput, and display the last message(but only the last one). How do i display all the messages from the RAWInput?? But not just the last one?

And how do i extract who the message is from?

I have limited the messages to the last 5.

connection.mam.query("[email protected]", {
  "with": "[email protected]","before": '',"max":"5",
  onMessage: function(message) {


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




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

  }
    });
Share edited Aug 27, 2015 at 8:54 John asked Aug 27, 2015 at 6:25 JohnJohn 1,0032 gold badges14 silver badges32 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

You can get all the messages using the `strophe.mam.js plugin

Here is my working code:

// 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);
        });
});
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./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);
    }
});

The easiest way to retrieve all the messages is to include this line at the end of the onMessage() function:

return true;

I think the reason is that if a handler does not return true, it will be destroyed after the first time it is called.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744928026a4601561.html

相关推荐

  • javascript - Strophe MAM how to display the messages? - Stack Overflow

    I have managed to use Strophe MAM to get the archived messages into the RAWInput, and display the last

    2天前
    70

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信