在线和离线用户实时使用strophe.js [英] Online and Offline users using strophe.js in real time
问题描述
我正在使用 strophe.js javascript客户端库,使用以下代码连接 xmpp 服务器( openfire )。
I am using strophe.js javascript client library for connecting to xmpp server(openfire) using below code.
var BOSH_SERVICE = 'http://127.0.0.1:7070/http-bind/';
connection = new Strophe.Connection(BOSH_SERVICE);
connection.connect("jid",
"password",
onConnect);
和回调函数(onConnect)如下:
and callback function(onConnect) as below :
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());
}
}
我成功连接到服务器使用此代码和在此之前没有问题。
i am successfully connect to server using this code and no problem until this.
问题:实时更新状态状态的用户列表。
Problem : updating user list with status in real time.
让我解释一下我的问题:
Let me explain my problem :
我想显示实时更新的在线和离线用户列表。(与显示聊天应用程序类似的东西。)
I want to show list of online and offline users with real time update.(something similar to showing chat apps.)
ex。假设有3个用户A,B和C.并且所有用户都在线(登录)
ex. Suppose there is 3 users A,B and C. and all are online (logged-in)
现在假设用户A断开连接或脱机然后用户B,C如何收到用户A?的状态通知。并且在用户B和C列表中没有刷新的情况下将用户A的状态更改为离线。
Now suppose user A get disconnect or go offline then how user B,C get notified with status of user A ?. and change status of user A to offline without refresh in user B and C list.
strophe.js中是否有任何方法会在某个连接时自动调用或断开连接。或者我应该自己编写吗?
is there is any method in strophe.js that will automatically call when some one get connect or dis-connect. or should i need to write my own?
我不确定但名单上有什么。
i am not sure but there is something with roster.
推荐答案
您可以使用此Strophe API为您的好友订阅状态:
You can subscribe to presence for your buddies using this Strophe API:
connection.send($pres({
to: jid, // buddy jid
type: "subscribe"
}));
实现XMPP规范(参见 https://xmpp.org/rfcs/rfc3921.html#int 了解详情)。
好友可以接受订阅回复:
which implements the XMPP specification (see https://xmpp.org/rfcs/rfc3921.html#int for details). The buddy can accept subscription replying with:
connection.send($pres({
to: from, // jid of subscriber
type: "subscribed"
}));
您可以在Plunker上查看基于XMPP(使用Strophe.js)的Web客户端示例:
You can check my web client example based on XMPP (using Strophe.js) on Plunker:
http://plnkr.co/edit/EhQHDsYpDhrECmaaIlZO
这篇关于在线和离线用户实时使用strophe.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!