Firebase显示其他用户'除您之外的用户名使用在线状态

时间:2014-08-11 05:48:53

标签: firebase

嗨,我是firebase的新手,正在尝试firebase上的存在示例,一切正常。我的问题是如何显示其他人的用户名,因为我似乎无法找到解决方案,因为

  1. 我尝试使用谷歌搜索答案,但结果都不是我想要的。
  2. 我是Firebase和非mysql数据库的新手,所以我不知道如何在firebase上做WHERE声明
  3. 这是我的代码:

    <body>
    
    <div id="presenceDiv" class="l-demo-container example-base">
    </div>
    
    <script>
    
      var name = "<?php echo $uname;?>";
      var currentStatus = "★ online";
    
      // Get a reference to the presence data in Firebase.
      var userListRef = new Firebase("https://<URL>.firebaseio.com/");
    
      // Generate a reference to a new location for my user with push.
      var myUserRef = userListRef.push();
    
      // Get a reference to my own presence status.
      var connectedRef = new Firebase("https://<URL>.firebaseio.com//.info/connected");
    
      connectedRef.on("value", function(isOnline) {
        if (isOnline.val()) {
          // If we lose our internet connection, we want ourselves removed from the list.
          myUserRef.onDisconnect().remove();
    
          // Set our initial online status.
          setUserStatus("★ online");
        }
        else {
    
          // We need to catch anytime we are marked as offline and then set the correct status. We
          // could be marked as offline 1) on page load or 2) when we lose our internet connection
          // temporarily.
          setUserStatus(currentStatus);
        }
      });
    
      // A helper function to let us set our own state.
      function setUserStatus(status) {
        // Set our status in the list of online users.
        currentStatus = status;
        myUserRef.set({ name: name, status: status });
      }
    
      function getMessageId(snapshot) {
        return snapshot.name().replace(/[^a-z0-9\-\_]/gi,'');
      }
    
      // Update our GUI to show someone"s online status.
      userListRef.on("child_added", function(snapshot) {
        var user = snapshot.val();
    
        $("<div/>")
          .attr("id", getMessageId(snapshot))
          .text(user.name + " is currently " + user.status)
          .appendTo("#presenceDiv");
      });
    
      // Update our GUI to remove the status of a user who has left.
      userListRef.on("child_removed", function(snapshot) {
        $("#presenceDiv").children("#" + getMessageId(snapshot))
          .remove();
      });
    
      // Update our GUI to change a user"s status.
      userListRef.on("child_changed", function(snapshot) {
        var user = snapshot.val();
        $("#presenceDiv").children("#" + getMessageId(snapshot))
          .text(user.name + " is currently " + user.status);
      });
    
      // Use idle/away/back events created by idle.js to update our status information.
      document.onIdle = function () {
        setUserStatus("☆ idle");
      }
      document.onAway = function () {
        setUserStatus("☄ away");
      }
      document.onBack = function (isIdle, isAway) {
        setUserStatus("★ online");
      }
    
      setIdleTimeout(5000);
      setAwayTimeout(10000);
    </script>
    </body>
    </html>
    

    此脚本继续沿着我尝试登录的其他虚拟用户加载我的第一个虚拟用户名。其他虚拟账户也是如此,浏览器加载他们的用户名和其他人。这是什么导致这个,我该如何解决?请帮忙

1 个答案:

答案 0 :(得分:3)

我只是识别并排除您on(child_处理程序中的当前用户。

例如:

// Update our GUI to show someone"s online status.
userListRef.on("child_added", function(snapshot) {
  var user = snapshot.val();

  if (user.name != name) {
    $("<div/>")
      .attr("id", getMessageId(snapshot))
      .text(user.name + " is currently " + user.status)
      .appendTo("#presenceDiv");
  }
});