4

在我基于 Firebase 的聊天应用程序中,我希望用户能够知道其他用户是否在任何设备(浏览器/iOS/Android)上在线。

我参考了此页面上显示的示例。但是使用这个例子,我注意到创建了多个连接密钥,但没有被正确删除。所以我最终增强了这样的例子:

function _managePresence(uid) {
  // top level presence node
  var ref = coreService.presence.child(uid);
  // stores connections from various browsers or devices
  var myConnectionsRef = ref.child('connections');
  // stores the timestamp of my last disconnect (the last time I was seen online)
  var lastOnlineRef = ref.child('lastOnline');
  // this one is special, it tells locally to the device, if I'm connected or not
  var connectedRef = coreService.connection;

  // add this device to my connections list
  var addConnection = function(myConnectionsRef) {
    // TODO: this value could contain info about the device or a timestamp too
    var con = myConnectionsRef.push(true);

    // save the connection key (con.key) locally also
    console.log("saving connection key", con.key);
    localStorage.setItem('currentConnectionKey', con.key);

    // when I disconnect, remove this connection key (from firebase)
    con.onDisconnect().remove();
  };

  connectedRef.on('value', function(snap) {
    if (snap.val() === true) {
      // We're connected (or reconnected)! Do anything here that should happen only if online (or on reconnect)

      // retrieve connection key from local storage
      var connectionKey = localStorage.getItem('currentConnectionKey');
      console.log("connection key found", connectionKey);

      if (connectionKey) {
        // connection key already exists locally, lets check firebase
        // NOTE: the use of once here, we don't want on('value')
        // because when the connection is removed, on('value') will get called
        // with snap.val() being null and we dont want to create a new connection key in that case
        // new connection key should only be created when we see a change in connectedRef.on('value')
        myConnectionsRef.child(connectionKey).once('value', function (snap) {
          if (snap.val() === true) {
            // connection key already exists in firebase also, we wont add a new connection key
            console.log("key already exists locally and in firebase", connectionKey);
          } else {
            // this connection key does not exist in firebase, lets create a new connection
            console.log("key not found in firebase, lets add a new one");
            addConnection(myConnectionsRef);
          }
        });
      } else {
        console.log("First time, lets addConnection");
        addConnection(myConnectionsRef);
      }

      // when I disconnect, update the last time I was seen online
      lastOnlineRef.onDisconnect().set(firebase.database.ServerValue.TIMESTAMP);
    } else {
      console.log("disconnecting...");
    }
  });
}

要知道某人是否在线,我只需查看 /presence/uid/connections 的孩子数

  _isOnline() {
    var ref = coreService.presence.child(this.uid);
    var myConnectionsRef = ref.child('connections');
    var self = this;

    myConnectionsRef.on('value', function (snap) {
      if (snap.hasChildren()) self.isON = true;
      else self.isON = false;
    });
  }

  _lastOnline() {
    var ref = coreService.presence.child(this.uid);
    var lastOnlineRef = ref.child('lastOnline');
    var self = this;

    lastOnlineRef.on( 'value',  function (snap) {
      self.lastOnlineTime = snap.val();
    });
  }

这是确定用户是否在线的正确方法吗?这可能是太多无法阅读的代码,但如果有人可以帮助我验证它,我将不胜感激。

4

0 回答 0