gpt4 book ai didi

javascript - 在页面加载 Strophe js 时检索消息

转载 作者:行者123 更新时间:2023-11-30 16:44:27 25 4
gpt4 key购买 nike

每当 Uses 登录并加载页面时,我都会尝试检索消息。现在我可以在用户将消息发送给其他用户时检索消息,即使用 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);

});

所以在我的代码中发生的事情是每当用户收到消息时,然后在这两个用户之间检索所有消息。

单击特定用户时如何检索聊天框中的消息?

最佳答案

这有两个问题。一个是当我在加载脚本时使用 login() 方法执行自动登录时,至少需要 800 毫秒才能登录并执行其他操作。在这一点上,我面临着这个问题,也参与了 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);
});

关于javascript - 在页面加载 Strophe js 时检索消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31454673/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com