Strophe MAM 如何显示消息?
Strophe MAM how to display the messages?
我已经设法使用 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");
}
});
您可以使用`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);
});
});
检索所有消息的最简单方法是在 onMessage()
函数末尾包含这一行:
return true;
我认为原因是如果处理程序没有return true
,它将在第一次调用后被销毁。
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);
}
});
我已经设法使用 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");
}
});
您可以使用`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);
});
});
检索所有消息的最简单方法是在 onMessage()
函数末尾包含这一行:
return true;
我认为原因是如果处理程序没有return true
,它将在第一次调用后被销毁。
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);
}
});