Firebase 事件“<user> 正在写入...”,例如 WhatsApp

Firebase event "<user> is writing..." like WhatsApp

有什么方法可以在 Firebase 事件中克隆 whatsapp 事件“[用户] 正在写...”?

我在 https://www.firebase.com/docs/web/api/ 中阅读了有关 Firebase 事件的信息,但我没有找到任何有关该问题的信息。

谢谢。

您可以在聊天信息中设置标志

例如:

{ "chat1 : { "name: "Jon", "isWriting" : true } "}

发送消息时将布尔值更改为 "false" 如果不再连接到 Internet,请设置一个时间将布尔值更改为 false。

前阵子写了这样一个打字指示器

var ref = new Firebase('https://<your-app>.firebaseio.com');
var input = document.getElementById('input');
var typers = document.getElementById('typers');
var uid = Date.now(); // generate a fake user id
var timer;

// attach a listener that display all people current typing in a list
ref.on('value', function(snapshot) {
  typers.innerText = '';
  snapshot.forEach(function(typer) {
    var li = document.createElement('li');
    li.innerText = typer.key();
    typers.appendChild(li);
  });
});

// whenever the content of the textarea changes
input.addEventListener('input',function(e) {
  // mark this user a "typing"
  ref.child(uid).set(true);
  // if we're counting down, stop the timer
  if (timer) clearTimeout(timer);
  // remove this user in 2 seconds
  timer = setTimeout(function() {
    ref.child(uid).remove();
  }, 2000);
});

要查看实际效果,请查看 this JSBintweet where I announced it.