Javascript 适用于多个设备的Firebase显示系统

Javascript 适用于多个设备的Firebase显示系统,javascript,firebase,firebase-realtime-database,user-presence,Javascript,Firebase,Firebase Realtime Database,User Presence,在我基于Firebase的聊天应用程序中,我希望用户能够知道其他用户是否在任何一台设备(浏览器/iOS/Android)上在线 我参考了上面所示的样品。但在这个例子中,我注意到创建了多个连接键,但没有正确删除。因此,我最终增强了示例,如下所示: function _managePresence(uid) { // top level presence node var ref = coreService.presence.child(uid); // stores connectio

在我基于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();
    });
  }

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

我也有同样的问题,这段代码对你有用吗?您是否再次看到未删除的连接?这很好,谢谢您的发布。关于这个话题的信息并不多。我发现这篇文章也很有帮助: