2016-08-17 76 views
3

我使用strophe.js JavaScript客户端库,用于连接到使用下面的代码XMPP服务器的Openfire)。在线和离线用户使用strophe.js实时

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任何方法,它会自动调用时,有一个人得到连接或DIS-连接。或者我需要写我自己的?

我不能肯定,但也有一些是有名册。

回答

5

您可以订阅存在使用该API的strophe您的好友:

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检查(使用Strophe.js)基于XMPP我的Web客户端例如:

http://plnkr.co/edit/EhQHDsYpDhrECmaaIlZO

+0

感谢您的答复。我只需添加订阅插件即可开启并设置自动订阅者。现在它的工作正常...你的答案也是我想知道的。谢谢哥们。 –

+0

是否有可能使用strophe.js或其他任何使用openfire的lib创建视频聊天? –

+0

我只能建议你检查的WebRTC功能,允许网页浏览器做实时音频/同行之间的视频通信 – beaver