在页面加载 Strophe js 上检索消息
Posted
技术标签:
【中文标题】在页面加载 Strophe js 上检索消息【英文标题】:Retrieve messages on Page load Strophe js 【发布时间】:2015-07-16 12:50:54 【问题描述】:每当使用登录并加载页面时,我都会尝试检索消息。现在,只要用户将消息发送给其他用户,即使用 onMessage 函数,我就可以检索消息。
这是我的代码:
var archive = [];
// Retrives the messages whenever User sends the messages to Other User.
// TODO: Should be better when User logins and page loads
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]),
avatar:'images/default_avatar_image.png'
;
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,data:data,response:response);
;
console.log('xmpp.history.start',query:q);
function onMessage(msg)
// Calls whenever User receives the messages
// and shows the received message in messagebox
var fromJid = msg.getAttribute("from"),
bareFromJid = Strophe.getBareJidFromJid(fromJid),
type = msg.getAttribute("type"),
elems = msg.getElementsByTagName("body");
if (type == "chat" && elems.length > 0)
var body = elems[0],
message = Strophe.getText(body);
showMessage(bareFromJid + ": " + message);
connection.mam.query(Strophe.getBareJidFromJid(connection.jid), q);
return true;
function send()
// Calls whenever User sends the messages
// and shows the message in messagebox
var to = $('#to-jid').get(0).value,
myBareJid = Strophe.getBareJidFromJid(connection.jid);
message = $('#message').val(),
reply = $msg(to: to, type: 'chat')
.c("body")
.t(message);
connection.send(reply.tree());
showMessage(myBareJid + ": " + message);
$(document).ready(function ()
connection = new Strophe.Connection(BOSH_SERVICE);
archive = $("#archive-messages");
archive.val("");
connection.rawInput = function (data) log('RECV: ' + data); ;
connection.rawOutput = function (data) log('SEND: ' + data); ;
Strophe.log = function (level, msg) log('LOG: ' + msg); ;
$("#send").bind('click', send);
);
所以在我的代码中发生的情况是,每当用户收到消息时,就会在这两个用户之间检索所有消息。
当我点击特定用户时,如何检索聊天框中的消息??
【问题讨论】:
【参考方案1】:这有两个问题。一种是当我在脚本加载时使用login()
方法执行自动登录时,至少需要800ms 才能登录并执行其他操作。在这一点上,我面临着这个问题,也有点 jQuery 部分。
这是我的代码:
// Retrives the messages between two particular users.
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])
;
var login_user = connection.jid.split('@')[0];
var username = msg_data.from.split('@')[0];
if(login_user == username)
archive.val(archive.val() + "me:".capitalizeFirstLetter() + msg_data.message + "\n");
archive.scrollTop(archive[0].scrollHeight - archive.height());
else
archive.val(archive.val() + username.capitalizeFirstLetter() + ":" + 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);
;
$("#to-jid").change(function()
$("#archive-messages").val("");
var to = "with": $(this).val();
$.extend(q, to);
// It takes around 800ms to 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);
);
【讨论】:
您好,我收到错误“未捕获的类型错误:无法读取未定义的属性‘查询’”。你能帮忙吗? @gaurang 您在哪一行收到错误消息?你能粘贴错误回溯吗? 在 connection.mam.query(Strophe.getBareJidFromJid(connection.jid), q);行和错误是“未捕获的类型错误:无法读取未定义的属性'查询'。我问过***.com/questions/48317459/… 和github.com/strophe/strophejs-plugin-mam/issues/8以上是关于在页面加载 Strophe js 上检索消息的主要内容,如果未能解决你的问题,请参考以下文章