在我的应用程序中,在单击按钮的某个点上,该应用程序需要在RTDB上执行操作(具体来说,检查节点是否存在)。为了确保用户未连接到网络时得到警告,我正在像这样检查
private fun checkNet(): Boolean {
val cm = context?.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val netC = cm.getNetworkCapabilities(cm.activeNetwork)
return (
netC != null &&
netC.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) &&
netC.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED) &&
(netC.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) ||
netC.hasTransport(NetworkCapabilities.TRANSPORT_WIFI ) )
)
}
,然后在必要时调用该函数。
但是我面临的问题是,如果互联网连接丢失然后重新获得,那么即使连接了侦听器,它也不会连接到服务器。我意识到这很花时间,但是连接到.info/connected
位置的监听器在连接时永远不会触发
override fun onStop() {
super.onStop()
Firebase.database.getReference(".info/connected").removeEventListener(listener)
}
override fun onStart() {
super.onStart()
Firebase.database.getReference(".info/connected").addValueEventListener(listener)
}
object listener: ValueEventListener {
override fun onCancelled(error: DatabaseError) = Unit
override fun onDataChange(snapshot: DataSnapshot) {
val isCon = if (snapshot.value != null) snapshot.value as Boolean
else false
if (isCon) Log.d(TAG, "connected".err())
else Log.d(TAG, "not connected".err())
}
}
即使通过离开应用程序并返回到应用程序来触发onStart()
,监听器也不会登录connected
。仅当我关闭整个应用程序然后重新打开它时,它才会连接。
所以,我的问题是我是否假设.info/connected
的行为正确,如果可以,那么在存在一个互联网连接?
编辑-按照@FrankVanPuffelen的建议,我继续进行调试日志记录。导航到该片段并在存在Internet连接的情况下访问数据库后,日志输出为
2020-07-03 15:58:50.167 com.example.paylater D/PersistentConnection: pc_0 - Listening on sellers/2 (params: {})
2020-07-03 15:58:50.167 com.example.paylater D/PersistentConnection: pc_0 - Adding listen query: sellers/2 (params: {})
2020-07-03 15:58:50.167 com.example.paylater D/Connection: conn_0 - Sending data: {t=d, d={a=q, r=20, b={p=sellers/2, h=}}}
2020-07-03 15:58:50.167 com.example.paylater D/WebSocket: ws_0 - Reset keepAlive. Remaining: 41278
2020-07-03 15:58:50.588 com.example.paylater D/WebSocket: ws_0 - ws message: {"t":"d","d":{"r":20,"b":{"s":"ok","d":{}}}}
2020-07-03 15:58:50.589 com.example.paylater D/WebSocket: ws_0 - Reset keepAlive. Remaining: 44578
2020-07-03 15:58:50.589 com.example.paylater D/WebSocket: ws_0 - HandleNewFrameCount: 1
2020-07-03 15:58:50.590 com.example.paylater D/WebSocket: ws_0 - handleIncomingFrame complete frame: {t=d, d={r=20, b={d={}, s=ok}}}
2020-07-03 15:58:50.590 com.example.paylater D/Connection: conn_0 - received data message: {r=20, b={d={}, s=ok}}
2020-07-03 15:58:50.591 com.example.paylater D/EventRaiser: Raising 1 event(s)
2020-07-03 15:58:50.591 com.example.paylater D/EventRaiser: Raising /sellers/2: VALUE: null
2020-07-03 15:58:50.620 com.example.paylater D/PersistentConnection: pc_0 - unlistening on sellers/2 (params: {})
2020-07-03 15:58:50.620 com.example.paylater D/PersistentConnection: pc_0 - removing query sellers/2 (params: {})
2020-07-03 15:58:50.620 com.example.paylater D/Connection: conn_0 - Sending data: {t=d, d={a=n, r=21, b={p=sellers/2}}}
2020-07-03 15:58:50.621 com.example.paylater D/WebSocket: ws_0 - Reset keepAlive. Remaining: 44968
2020-07-03 15:58:51.228 com.example.paylater D/WebSocket: ws_0 - ws message: {"t":"d","d":{"r":21,"b":{"s":"ok","d":""}}}
2020-07-03 15:58:51.228 com.example.paylater D/WebSocket: ws_0 - Reset keepAlive. Remaining: 44392
2020-07-03 15:58:51.228 com.example.paylater D/WebSocket: ws_0 - HandleNewFrameCount: 1
2020-07-03 15:58:51.229 com.example.paylater D/WebSocket: ws_0 - handleIncomingFrame complete frame: {t=d, d={r=21, b={d=, s=ok}}}
2020-07-03 15:58:51.229 com.example.paylater D/Connection: conn_0 - received data message: {r=21, b={d=, s=ok}}
禁用移动数据30秒钟,然后启用它并再次访问RTDB之后,输出为
2020-07-03 15:59:30.260 com.example.paylater D/PersistentConnection: pc_0 - Listening on sellers/2 (params: {})
2020-07-03 15:59:30.261 com.example.paylater D/PersistentConnection: pc_0 - Adding listen query: sellers/2 (params: {})
2020-07-03 15:59:30.262 com.example.paylater D/Connection: conn_0 - Sending data: {t=d, d={a=q, r=22, b={p=sellers/2, h=}}}
2020-07-03 15:59:30.263 com.example.paylater D/WebSocket: ws_0 - Reset keepAlive. Remaining: 5965
2020-07-03 16:00:15.263 com.example.paylater D/WebSocket: ws_0 - Reset keepAlive. Remaining: 0
2020-07-03 16:00:51.226 com.example.paylater D/WebSocket: ws_0 - WebSocket reached EOF.
2020-07-03 16:00:51.226 com.example.paylater D/WebSocket: ws_0 - closing itself
2020-07-03 16:00:51.226 com.example.paylater D/Connection: conn_0 - Realtime connection lost
2020-07-03 16:00:51.226 com.example.paylater D/Connection: conn_0 - closing realtime connection
2020-07-03 16:00:51.226 com.example.paylater D/PersistentConnection: pc_0 - Got on disconnect due to OTHER
2020-07-03 16:00:51.226 com.example.paylater D/PersistentConnection: pc_0 - Scheduling connection attempt
2020-07-03 16:00:51.226 com.example.paylater D/ConnectionRetryHelper: Scheduling retry in 0ms
2020-07-03 16:00:51.227 com.example.paylater D/EventRaiser: Raising 1 event(s)
2020-07-03 16:00:51.227 com.example.paylater D/EventRaiser: Raising /.info/connected: VALUE: false
2020-07-03 16:00:51.227 com.example.paylater D/PersistentConnection: pc_0 - Trying to fetch auth token
2020-07-03 16:00:51.229 com.example.paylater D/WebSocket: ws_0 - WebSocket error.
com.google.firebase.database.tubesock.WebSocketException: IO Exception
at com.google.firebase.database.tubesock.WebSocketWriter.runWriter(WebSocketWriter.java:159)
at com.google.firebase.database.tubesock.WebSocketWriter.access$000(WebSocketWriter.java:30)
at com.google.firebase.database.tubesock.WebSocketWriter$1.run(WebSocketWriter.java:47)
at java.lang.Thread.run(Thread.java:919)
Caused by: java.net.SocketException: Socket is closed
at com.android.org.conscrypt.AbstractConscryptSocket.checkOpen(AbstractConscryptSocket.java:581)
at com.android.org.conscrypt.ConscryptFileDescriptorSocket$SSLOutputStream.write(ConscryptFileDescriptorSocket.java:609)
at java.nio.channels.Channels$WritableByteChannelImpl.write(Channels.java:453)
at com.google.firebase.database.tubesock.WebSocketWriter.writeMessage(WebSocketWriter.java:138)
at com.google.firebase.database.tubesock.WebSocketWriter.runWriter(WebSocketWriter.java:152)
at com.google.firebase.database.tubesock.WebSocketWriter.access$000(WebSocketWriter.java:30)
at com.google.firebase.database.tubesock.WebSocketWriter$1.run(WebSocketWriter.java:47)
at java.lang.Thread.run(Thread.java:919)
2020-07-03 16:00:51.229 com.example.paylater D/PersistentConnection: pc_0 - Successfully fetched token, opening connection
2020-07-03 16:00:51.230 com.example.paylater D/Connection: conn_1 - Opening a connection
2020-07-03 16:00:51.233 com.example.paylater D/WebSocket: ws_0 - closed
2020-07-03 16:00:52.825 com.example.paylater D/WebSocket: ws_1 - websocket opened
2020-07-03 16:00:52.825 com.example.paylater D/WebSocket: ws_1 - Reset keepAlive
2020-07-03 16:00:52.826 com.example.paylater D/WebSocket: ws_1 - ws message: {"t":"c","d":{"t":"h","d":{"ts":1593772513254,"v":"5","h":"s-usc1c-nss-203.firebaseio.com","s":"QMthzsHOAtBUR6Q05YGUpSKLJIeW1z6J"}}}
2020-07-03 16:00:52.826 com.example.paylater D/WebSocket: ws_1 - Reset keepAlive. Remaining: 44998
2020-07-03 16:00:52.826 com.example.paylater D/WebSocket: ws_1 - HandleNewFrameCount: 1
2020-07-03 16:00:52.827 com.example.paylater D/WebSocket: ws_1 - handleIncomingFrame complete frame: {t=c, d={t=h, d={h=s-usc1c-nss-203.firebaseio.com, s=QMthzsHOAtBUR6Q05YGUpSKLJIeW1z6J, v=5, ts=1593772513254}}}
2020-07-03 16:00:52.827 com.example.paylater D/Connection: conn_1 - Got control message: {t=h, d={h=s-usc1c-nss-203.firebaseio.com, s=QMthzsHOAtBUR6Q05YGUpSKLJIeW1z6J, v=5, ts=1593772513254}}
2020-07-03 16:00:52.827 com.example.paylater D/Connection: conn_1 - realtime connection established
2020-07-03 16:00:52.827 com.example.paylater D/PersistentConnection: pc_0 - onReady
2020-07-03 16:00:52.827 com.example.paylater D/PersistentConnection: pc_0 - handling timestamp
2020-07-03 16:00:52.827 com.example.paylater D/PersistentConnection: pc_0 - calling restore state
2020-07-03 16:00:52.827 com.example.paylater D/PersistentConnection: pc_0 - Restoring auth.
2020-07-03 16:00:52.827 com.example.paylater D/Connection: conn_1 - Sending data (contents hidden)
2020-07-03 16:00:52.827 com.example.paylater D/WebSocket: ws_1 - Reset keepAlive. Remaining: 44998
2020-07-03 16:00:52.828 com.example.paylater D/EventRaiser: Raising 1 event(s)
2020-07-03 16:00:52.829 com.example.paylater D/EventRaiser: Raising /.info/connected: VALUE: true
2020-07-03 16:00:53.466 com.example.paylater D/WebSocket: ws_1 - ws message: {"t":"d","d":{"r":23,"b":{"s":"ok","d":{"auth":{"name":"Yash Singh","email_verified":false,"provider":"password","email":"yashsingh.real@gmail.com","user_id":"QBO65tn5qLVN8zyVB2uMA8UByJ02","token":{"name":"Yash Singh","email_verified":false,"email":"yashsingh.real@gmail.com","exp":1593775621,"user_id":"QBO65tn5qLVN8zyVB2uMA8UByJ02","iat":1593772021,"sub":"QBO65tn5qLVN8zyVB2uMA8UByJ02","aud":"the-creditinator","auth_time":1592475916,"iss":"https://securetoken.google.com/the-creditinator","firebase":{"identities":{"email":["yashsingh.real@gmail.com"]},"sign_in_provider":"password"}},"uid":"QBO65tn5qLVN8zyVB2uMA8UByJ02"},"expires":1593775621}}}}
2020-07-03 16:00:53.467 com.example.paylater D/WebSocket: ws_1 - Reset keepAlive. Remaining: 44360
2020-07-03 16:00:53.467 com.example.paylater D/WebSocket: ws_1 - HandleNewFrameCount: 1
2020-07-03 16:00:53.467 com.example.paylater D/WebSocket: ws_1 - handleIncomingFrame complete frame: {t=d, d={r=23, b={d={auth={uid=QBO65tn5qLVN8zyVB2uMA8UByJ02, email_verified=false, provider=password, user_id=QBO65tn5qLVN8zyVB2uMA8UByJ02, name=Yash Singh, email=yashsingh.real@gmail.com, token={sub=QBO65tn5qLVN8zyVB2uMA8UByJ02, aud=the-creditinator, email_verified=false, user_id=QBO65tn5qLVN8zyVB2uMA8UByJ02, auth_time=1592475916, name=Yash Singh, iss=https://securetoken.google.com/the-creditinator, exp=1593775621, firebase={sign_in_provider=password, identities={email=[yashsingh.real@gmail.com]}}, iat=1593772021, email=yashsingh.real@gmail.com}}, expires=1593775621}, s=ok}}}
2020-07-03 16:00:53.468 com.example.paylater D/Connection: conn_1 - received data message: {r=23, b={d={auth={uid=QBO65tn5qLVN8zyVB2uMA8UByJ02, email_verified=false, provider=password, user_id=QBO65tn5qLVN8zyVB2uMA8UByJ02, name=Yash Singh, email=yashsingh.real@gmail.com, token={sub=QBO65tn5qLVN8zyVB2uMA8UByJ02, aud=the-creditinator, email_verified=false, user_id=QBO65tn5qLVN8zyVB2uMA8UByJ02, auth_time=1592475916, name=Yash Singh, iss=https://securetoken.google.com/the-creditinator, exp=1593775621, firebase={sign_in_provider=password, identities={email=[yashsingh.real@gmail.com]}}, iat=1593772021, email=yashsingh.real@gmail.com}}, expires=1593775621}, s=ok}}
2020-07-03 16:00:53.468 com.example.paylater D/PersistentConnection: pc_0 - Restoring outstanding listens
2020-07-03 16:00:53.468 com.example.paylater D/PersistentConnection: pc_0 - Restoring listen sellers/2 (params: {})
2020-07-03 16:00:53.468 com.example.paylater D/Connection: conn_1 - Sending data: {t=d, d={a=q, r=24, b={p=sellers/2, h=}}}
2020-07-03 16:00:53.469 com.example.paylater D/WebSocket: ws_1 - Reset keepAlive. Remaining: 44997
2020-07-03 16:00:53.471 com.example.paylater D/PersistentConnection: pc_0 - Restoring writes.
2020-07-03 16:00:54.105 com.example.paylater D/WebSocket: ws_1 - ws message: {"t":"d","d":{"r":24,"b":{"s":"ok","d":{}}}}
2020-07-03 16:00:54.105 com.example.paylater D/WebSocket: ws_1 - Reset keepAlive. Remaining: 44364
2020-07-03 16:00:54.106 com.example.paylater D/WebSocket: ws_1 - HandleNewFrameCount: 1
2020-07-03 16:00:54.106 com.example.paylater D/WebSocket: ws_1 - handleIncomingFrame complete frame: {t=d, d={r=24, b={d={}, s=ok}}}
2020-07-03 16:00:54.106 com.example.paylater D/Connection: conn_1 - received data message: {r=24, b={d={}, s=ok}}
2020-07-03 16:00:54.106 com.example.paylater D/EventRaiser: Raising 1 event(s)
2020-07-03 16:00:54.107 com.example.paylater D/EventRaiser: Raising /sellers/2: VALUE: null
2020-07-03 16:00:54.107 com.example.paylater D/PersistentConnection: pc_0 - unlistening on sellers/2 (params: {})
2020-07-03 16:00:54.107 com.example.paylater D/PersistentConnection: pc_0 - removing query sellers/2 (params: {})
2020-07-03 16:00:54.108 com.example.paylater D/Connection: conn_1 - Sending data: {t=d, d={a=n, r=25, b={p=sellers/2}}}
2020-07-03 16:00:54.108 com.example.paylater D/WebSocket: ws_1 - Reset keepAlive. Remaining: 44997
2020-07-03 16:00:54.632 com.example.paylater D/WebSocket: ws_1 - ws message: {"t":"d","d":{"r":25,"b":{"s":"ok","d":""}}}
2020-07-03 16:00:54.633 com.example.paylater D/WebSocket: ws_1 - Reset keepAlive. Remaining: 44474
2020-07-03 16:00:54.633 com.example.paylater D/WebSocket: ws_1 - HandleNewFrameCount: 1
2020-07-03 16:00:54.633 com.example.paylater D/WebSocket: ws_1 - handleIncomingFrame complete frame: {t=d, d={r=25, b={d=, s=ok}}}
2020-07-03 16:00:54.633 com.example.paylater D/Connection: conn_1 - received data message: {r=25, b={d=, s=ok}}
毕竟,它在大约一分钟后给出了输出,但是行为并不一致,我希望日志消息可以对此有所帮助。