gpt4 book ai didi

javascript - 在线和离线用户实时使用 strophe.js

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:00:50 28 4
gpt4 key购买 nike

我正在使用 strophe.js javascript 客户端库通过以下代码连接到 xmpp 服务器 (openfire)。

var BOSH_SERVICE = 'http://127.0.0.1:7070/http-bind/';
connection = new Strophe.Connection(BOSH_SERVICE);

connection.connect("jid",
"password",
onConnect);

和回调函数(onConnect)如下:

function onConnect(status)
{
if (status == Strophe.Status.CONNECTING) {
log('Strophe is connecting.');
} else if (status == Strophe.Status.CONNFAIL) {
log('Strophe failed to connect.');
$('#connect').get(0).value = 'connect';
} else if (status == Strophe.Status.DISCONNECTING) {
log('Strophe is disconnecting.');
} else if (status == Strophe.Status.DISCONNECTED) {
log('Strophe is disconnected.');
$('#connect').get(0).value = 'connect';
} else if (status == Strophe.Status.CONNECTED) {
log('Strophe is connected.');
log('ECHOBOT: Send a message to ' + connection.jid +
' to talk to me.');

connection.addHandler(onMessage, null, 'message', null, null, null);
connection.send($pres().tree());
console.log($pres().tree());

}
}

我使用此代码成功连接到服务器并且在此之前没有问题

问题:实时更新用户列表状态。

让我解释一下我的问题:

我想显示实时更新的在线和离线用户列表。(类似于显示聊天应用程序。)

例如。假设有3个用户A、B、C,都在线(已登录)

现在假设用户 A 断开连接或离线,那么用户 B、C 如何收到用户 A 状态的通知?并在用户B和C列表中将用户A的状态更改为离线,不刷新。

strophe.js 中是否有任何方法会在有人连接或断开连接时自动调用。还是我需要自己写?

我不确定,但花名册上有一些东西。

最佳答案

您可以使用此 Strophe API 为您的好友订阅状态:

connection.send($pres({
to: jid, // buddy jid
type: "subscribe"
}));

它实现了 XMPP 规范(详见 https://xmpp.org/rfcs/rfc3921.html#int)。好友可以接受订阅回复:

connection.send($pres({
to: from, // jid of subscriber
type: "subscribed"
}));

您可以在 Plunker 上查看我基于 XMPP(使用 Strophe.js)的 Web 客户端示例:

http://plnkr.co/edit/EhQHDsYpDhrECmaaIlZO

关于javascript - 在线和离线用户实时使用 strophe.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38988580/

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