Firebase:onDisconnect 事件何时触发? [英] Firebase: when onDisconnect event fire?

查看:14
本文介绍了Firebase:onDisconnect 事件何时触发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将 Firebase 后端用于我的 android 应用程序.我想为我的聊天构建一个用户在线系统.为此,我采用了 Firebase 指南中的模式

I`m using Firebase-backend for my android applicaion. I'd like to build a users presence system for my chat. For this purpose I've taken the pattern from Firebase Guide

final Firebase myConnectionsRef = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com/users/joe/connections");

// stores the timestamp of my last disconnect (the last time I was seen online)
final Firebase lastOnlineRef = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com/users/joe/lastOnline");

final Firebase connectedRef = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com/.info/connected");
connectedRef.addValueEventListener(new ValueEventListener() {
  @Override
  public void onDataChange(DataSnapshot snapshot) {
    boolean connected = snapshot.getValue(Boolean.class);
    if (connected) {
      // add this device to my connections list
      // this value could contain info about the device or a timestamp too
      Firebase con = myConnectionsRef.push();
      con.setValue(Boolean.TRUE);

      // when this device disconnects, remove it
      con.onDisconnect().removeValue();

      // when I disconnect, update the last time I was seen online
      lastOnlineRef.onDisconnect().setValue(ServerValue.TIMESTAMP);
    }
  }

  @Override
  public void onCancelled(FirebaseError error) {
    System.err.println("Listener was cancelled at .info/connected");
  }
});

问题是我不知道什么时候会触发 onDisconnect 事件?!

The trouble is I don't know when onDisconnect event would be fired?!

每次我打开应用程序时,我都会向节点users/joe/connections"写入 TRUE,但是当我关闭应用程序时,什么也不会发生.当我关闭 WIFI 时,布尔参数也不会被删除.onDisconnect-event 仅在我强行停止我的应用程序或重新安装此应用程序或其他我无法确定时触发.

Every time I open the app I write TRUE to the node "users/joe/connections" but when I closed app nothing would be happend. When I switched WIFI off then the boolean parameter wouldn't be removed as well. onDisconnect-event only fired when I forcely stopped my app or reinstalled this app or somewhen else when I couldn`t determine.

所以,据我所知,我必须手动处理此类事件:

So, as I understand right I have to handle manually such events:

1) 关闭我的应用;

2) 关闭 WiFi;

2) switch WiFi off;

3) 也许是别的东西

用于在我的应用程序中创建在线状态功能?但是什么时候有 onDisconnect-event 被触发?

for creating Presence feature in my app? But then when have onDisconnect-event to be fired ?

推荐答案

客户端和服务器断开连接可能会出现两种情况:

There are two cases that may happen when the client and server are disconnected:

  1. 客户端明确断开与服务器的连接
  2. 客户就这么消失了

当您终止应用程序时,您将触发显式断开连接.在这种情况下,客户端将向服务器发送一个断开连接的信号,服务器将立即执行 onDisconnect 回调.

When you kill the app, you are triggering an explicit disconnect. In that case the client will send a signal to the server that it is disconnecting and the server will immediately execute the onDisconnect callback.

当您关闭 wifi 时,您的应用没有机会告诉服务器它正在断开连接.在这种情况下,onDisconnect 将在服务器检测到客户端消失后触发.这可能需要几分钟,具体取决于套接字超时的配置方式.所以在这种情况下,你只需要多一点耐心.

When you switch off wifi, your app doesn't get a chance to tell the server that it is disconnecting. In that case the onDisconnect will fire once the server detects that the client is gone. This may take a few minutes, depending on how the time-outs for sockets are configured. So in that case you just have to be a bit more patient.

如果这个时间间隔对你来说不够细化,你能做的最好的事情就是定期更新数据库中所谓的保活值,更短的时间间隔.这样您就可以看到客户最近的活跃时间,如果时间太久,您可以采取行动.

It this interval is not granular enough for you, the best you can do is updates a so-called keep-alive value in the database at regular, shorter intervals. That way you can see when the client was most recently active, and act on that if it was too long ago.

这篇关于Firebase:onDisconnect 事件何时触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆