空闲时间过后,Firebase侦听器无法识别或恢复连接 [英] Firebase Listener does not identify or resume connection after idle time
问题描述
我的项目是确定客户端是联机还是脱机.我使用的是Android-Firebase文档中提供的代码,该文档在".info/connected"中使用了EventListener.
My project is to identify whether the client is online or offline. I use the code made available in the Android-Firebase documentation, which uses an EventListener in ".info / connected".
// since I can connect from multiple devices, we store each connection instance separately
// any time that connectionsRef's value is null (i.e. has no children) I am offline
final FirebaseDatabase database = FirebaseDatabase.getInstance();
final DatabaseReference myConnectionsRef = database.getReference("users/joe/connections");
// stores the timestamp of my last disconnect (the last time I was seen online)
final DatabaseReference lastOnlineRef = database.getReference("/users/joe/lastOnline");
final DatabaseReference connectedRef = database.getReference(".info/connected");
connectedRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
boolean connected = snapshot.getValue(Boolean.class);
if (connected) {
DatabaseReference con = myConnectionsRef.push();
// 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);
// add this device to my connections list
// this value could contain info about the device or a timestamp too
con.setValue(Boolean.TRUE);
}
}
@Override
public void onCancelled(DatabaseError error) {
System.err.println("Listener was cancelled at .info/connected");
}
});
此方法可以正确识别用户何时终止或重新打开应用程序,或者何时在Internet上发生较小的断开连接.但是,我进行了一次测试,将设备的Wifi断开了2分钟,它在未连接的Firebase数据库中被标识出来,但是当我打开Wifi并恢复互联网连接时,Firebase无法识别活动连接,似乎用户仍然保持不活动状态.
This method correctly identifies when the user terminates or reopens the application or when minor disconnections occur on the internet. However, I did a test turn off the Wifi of the device for a time of 2 minutes, it is identified in the Firebase Database that is not connected, but when I turn on the Wifi, resuming the internet connection, Firebase does not recognize the active connection, appears as if the user still remained inactive.
此代码段已插入MainActivity的onCreate方法中.
This code snippet was inserted into the onCreate method of my MainActivity.
谁能告诉我为什么会这样?
Can anyone tell me why this occurs?
编辑:附件
错误日志:
11-14 10:12:55.272 27901-27926/com.example.davi.projetofirebase D/PersistentConnection:pc_0-尝试获取身份验证令牌
11-14 10:12:55.272 27901-27926/com.example.davi.projetofirebase D/PersistentConnection: pc_0 - Trying to fetch auth token
11-14 10:12:55.276 27901-27926/com.example.davi.projetofirebase D/PersistentConnection:pc_0-成功获取令牌,打开连接
11-14 10:12:55.276 27901-27926/com.example.davi.projetofirebase D/PersistentConnection: pc_0 - Successfully fetched token, opening connection
11-14 10:12:55.281 27901-27926/com.example.davi.projetofirebase D/连接:conn_14-打开连接
11-14 10:12:55.281 27901-27926/com.example.davi.projetofirebase D/Connection: conn_14 - Opening a connection
11-14 10:12:55.293 27901-27926/com.example.davi.projetofirebase D/WebSocket:ws_14-WebSocket错误. bkx:未知主机:s-usc1c-nss-208.firebaseio.com 在bku.d(:com.google.android.gms.DynamiteModulesC@11746448:102) 在bkv.run(:com.google.android.gms.DynamiteModulesC@11746448:4) 在java.lang.Thread.run(Thread.java:760) 造成原因:java.net.UnknownHostException:s-usc1c-nss-208.firebaseio.com 在java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:174) 在java.net.SocksSocketImpl.connect(SocksSocketImpl.java:356) 在java.net.Socket.connect(Socket.java:586) 在com.android.org.conscrypt.OpenSSLSocketImpl.connect(OpenSSLSocketImpl.java:265) 在com.android.org.conscrypt.OpenSSLSocketImpl.connect(OpenSSLSocketImpl.java:252) 在java.net.Socket.(Socket.java:427) 在java.net.Socket.(Socket.java:210) 在javax.net.ssl.SSLSocket(SSLSocket.java:906) 在com.android.org.conscrypt.OpenSSLSocketImpl.(OpenSSLSocketImpl.java:192) 在com.android.org.conscrypt.OpenSSLSocketFactoryImpl.createSocket(OpenSSLSocketFactoryImpl.java:68) 在bku.d(:com.google.android.gms.DynamiteModulesC@11746448:94) 在bkv.run(:com.google.android.gms.DynamiteModulesC@11746448:4) 在java.lang.Thread.run(Thread.java:760)
11-14 10:12:55.293 27901-27926/com.example.davi.projetofirebase D/WebSocket: ws_14 - WebSocket error. bkx: unknown host: s-usc1c-nss-208.firebaseio.com at bku.d(:com.google.android.gms.DynamiteModulesC@11746448:102) at bkv.run(:com.google.android.gms.DynamiteModulesC@11746448:4) at java.lang.Thread.run(Thread.java:760) Caused by: java.net.UnknownHostException: s-usc1c-nss-208.firebaseio.com at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:174) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:356) at java.net.Socket.connect(Socket.java:586) at com.android.org.conscrypt.OpenSSLSocketImpl.connect(OpenSSLSocketImpl.java:265) at com.android.org.conscrypt.OpenSSLSocketImpl.connect(OpenSSLSocketImpl.java:252) at java.net.Socket.(Socket.java:427) at java.net.Socket.(Socket.java:210) at javax.net.ssl.SSLSocket.(SSLSocket.java:906) at com.android.org.conscrypt.OpenSSLSocketImpl.(OpenSSLSocketImpl.java:192) at com.android.org.conscrypt.OpenSSLSocketFactoryImpl.createSocket(OpenSSLSocketFactoryImpl.java:68) at bku.d(:com.google.android.gms.DynamiteModulesC@11746448:94) at bkv.run(:com.google.android.gms.DynamiteModulesC@11746448:4) at java.lang.Thread.run(Thread.java:760)
11-14 10:12:55.294 27901-27926/com.example.davi.projetofirebase D/WebSocket:ws_14-关闭自身
11-14 10:12:55.294 27901-27926/com.example.davi.projetofirebase D/WebSocket: ws_14 - closing itself
11-14 10:12:55.295 27901-27926/com.example.davi.projetofirebase D/连接:conn_14-实时连接失败
11-14 10:12:55.295 27901-27926/com.example.davi.projetofirebase D/Connection: conn_14 - Realtime connection failed
11-14 10:12:55.295 27901-27926/com.example.davi.projetofirebase D/连接:conn_14-关闭实时连接
11-14 10:12:55.295 27901-27926/com.example.davi.projetofirebase D/Connection: conn_14 - closing realtime connection
11-14 10:12:55.295 27901-27926/com.example.davi.projetofirebase D/PersistentConnection:pc_0-由于其他原因而断开连接
11-14 10:12:55.295 27901-27926/com.example.davi.projetofirebase D/PersistentConnection: pc_0 - Got on disconnect due to OTHER
11-14 10:12:55.296 27901-27926/com.example.davi.projetofirebase D/PersistentConnection:pc_0-计划连接尝试
11-14 10:12:55.296 27901-27926/com.example.davi.projetofirebase D/PersistentConnection: pc_0 - Scheduling connection attempt
11-14 10:12:55.297 27901-27926/com.example.davi.projetofirebase D/ConnectionRetryHelper:计划在27403毫秒内重试
11-14 10:12:55.297 27901-27926/com.example.davi.projetofirebase D/ConnectionRetryHelper: Scheduling retry in 27403ms
11-14 10:12:55.304 27901-27926/com.example.davi.projetofirebase D/WebSocket:ws_14-已关闭
11-14 10:12:55.304 27901-27926/com.example.davi.projetofirebase D/WebSocket: ws_14 - closed
11-14 10:13:02.947 27901-27926/com.example.davi.projetofirebase D/PersistentConnection:pc_0-连接中断:connection_idle
11-14 10:13:02.947 27901-27926/com.example.davi.projetofirebase D/PersistentConnection: pc_0 - Connection interrupted for: connection_idle
11-14 10:13:02.960 27901-27926/com.example.davi.projetofirebase D/ConnectionRetryHelper:取消现有的重试尝试
11-14 10:13:02.960 27901-27926/com.example.davi.projetofirebase D/ConnectionRetryHelper: Cancelling existing retry attempt
推荐答案
我联系了Firebase支持团队,他们告诉了我一个可以解决我问题的解决方案:
I contacted the support team Firebase, they informed me a solution that apparently solved my problem:
很明显,如果没有活动的活动或数据库的侦听器,则.info/connected的侦听器将停止侦听.我在数据库中的任何一个节点上添加了另一个辅助侦听器,并且可以正常工作!
Apparently the listener for .info / connected stops listening if there is no active action or listener for the database. I added another helper listener to any one node in my database, and it worked!
DatabaseReference myConnectionRef2 = database.getReference("/users/joe/lastOnline");
myConnectionRef2.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.getValue() == null) {
System.out.println("FIREBASE: Aux connected");
} else {
System.out.println("FIREBASE: Aux not connected");
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
每次我断开网络连接时,两个侦听器都会尝试恢复通信,直到重新连接网络为止.
Every time I lose the network connection, the two listeners try to resume communication until the network is reconnected.
感谢所有帮助我解决此问题的人.
Thanks to everyone who helped me solve this problem.
这篇关于空闲时间过后,Firebase侦听器无法识别或恢复连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!