【问题标题】:Online and Offline users using strophe.js in real time在线和离线用户实时使用 strophe.js
【发布时间】:2016-12-23 15:16:06
【问题描述】:

我正在使用 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 中是否有任何方法会在某人连接或断开连接时自动调用。还是我需要自己写?

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

【问题讨论】:

    标签: javascript xmpp openfire strophe


    【解决方案1】:

    您可以使用此 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

    【讨论】:

    • 感谢您的回复。我只是添加订阅插件来开火并设置自动订阅者。现在它工作正常......你的答案也是我想知道的。谢谢朋友。
    • 是否可以使用 strophe.js 或任何其他带有 openfire 的库来创建视频聊天?
    • 我只能建议您检查 WebRTC 功能,这些功能使 Web 浏览器能够在同行之间进行实时音频/视频通信
    • 因此您可以使用 XMPP 作为一种信令方式来控制实时通信(搜索 XMPP Jingle、strophe jingle...)
    • 谢谢.. 让我检查一下.. 我认为通过插件在 openfire 中 TURN 是可能的。
    猜你喜欢
    • 2012-05-23
    • 2020-07-02
    • 2014-05-30
    • 2019-01-26
    • 2012-01-17
    • 2020-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多