Ale*_*alo 10 android firebase firebase-realtime-database
我正在使用Firebase-backend用于我的android应用程序.我想为我的聊天建立一个用户在线系统.为此,我采用了Firebase指南中的模式
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");
}
});
Run Code Online (Sandbox Code Playgroud)
麻烦的是我不知道什么时候onDisconnect事件会被解雇?!
每次我打开应用程序时,我都会向节点" users/joe/connections " 写入TRUE,但是当我关闭app时,什么都不会发生.当我关闭WIFI时,布尔参数也不会被删除.onDisconnect-event仅在我强行停止我的应用程序或重新安装此应用程序时触发,或者在我无法确定时重新启动.
所以,据我所知,我必须手动处理这样的事件:
1)关闭我的应用程序;
2)关闭WiFi;
3)也许别的东西
在我的应用程序中创建Presence功能?但是当onDisconnect-event被解雇时呢?
Fra*_*len 16
客户端和服务器断开连接时可能会发生两种情况:
当您终止应用程序时,您将触发显式断开连接.在这种情况下,客户端将向服务器发送一个断开连接的信号,服务器将立即执行onDisconnect回调.
当您关闭wifi时,您的应用程序没有机会告诉服务器它正在断开连接.在这种情况下,onDisconnect一旦服务器检测到客户端消失,将触发.这可能需要几分钟,具体取决于套接字的超时配置方式.因此,在这种情况下,你只需要更耐心一点.
| 归档时间: |
|
| 查看次数: |
5564 次 |
| 最近记录: |