Firebase:何时onDisconnect事件触发? [英] Firebase: when onDisconnect event fire?
问题描述
$ b
最终Firebase myConnectionsRef =新的Firebase(https://< YOUR-火力-APP> .firebaseio.com /用户/乔/连接);
//存储最后一次断开连接的时间戳(最后一次在线上线)
最终Firebase lastOnlineRef =新建Firebase(https://< YOUR-FIREBASE-APP> .firebaseio.com /用户/乔/ LASTONLINE);
最终Firebase connectedRef =新的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){
//将此设备添加到我的连接列表
//此值可能包含有关设备的信息或时间戳
Firebase con = myConnectionsRef.push();
con.setValue(Boolean.TRUE);
//当此设备断开连接时,将其删除
con.onDisconnect()。removeValue();
//当断开连接时,更新上次在线上线的时间
lastOnlineRef.onDisconnect()。setValue(ServerValue.TIMESTAMP);
}
}
@Override
public void onCancelled(FirebaseError error){
System.err.println(Listener被取消.info / connected);
}
});
麻烦的是我不知道什么时候会打开 onDisconnect 事件每次打开应用程序时,我都会写TRUE给节点 users / joe / connections ,但是当我关闭应用程序的时候,什么都不会发生。
当我关闭WIFI时,布尔参数也不会被删除。
onDisconnect-event只有当我强行停止我的应用程序或重新安装这个应用程序或一些其他的时候,当我不能确定。
因此,据我所知我不得不手动处理这样的事件:
$ b $ 1)关闭我的应用程序;
$ b 2)关闭WiFi;
3)也许别的东西
用于在我的应用程序中创建Presence功能?但是当 onDisconnect-event 被解雇的时候呢? 解决方案
有两种情况可能发生当客户端和服务器断开连接时:
- 客户端显式断开与服务器的连接
- 客户端简单地消失
当您终止应用程序时,您触发了明确的断开连接。在这种情况下,客户端会发送一个信号给服务器,断开连接,服务器将立即执行 onDisconnect
回调。
当您关闭wifi时,您的应用程序不会有机会告诉服务器它正在断开连接。在这种情况下,一旦服务器检测到客户端消失, onDisconnect
就会触发。这可能需要几分钟时间,具体取决于套接字的超时配置。所以在这种情况下,你必须要有点耐心。
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");
}
});
The trouble is I don't know when onDisconnect event would be fired?!
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) close my app;
2) switch WiFi off;
3) maybe something else
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:
- the client explicitly disconnects from the server
- the client simply disappears
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.
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.
这篇关于Firebase:何时onDisconnect事件触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!