[英]How to signal to the database that I have lost the connection - Java
How can i make that if player1 loses connection, firebase will get signal, i just need to know how to make firebase get notification that player1 lost connection for example如果 player1 失去连接,我怎么能做到这一点,firebase 会收到信号,我只需要知道如何让 firebase 收到例如 player1 失去连接的通知
I use this to check if there is a connection and with this you can do all this, but from here I will not be able to transfer the sign to Firebase that I have lost the connection, because in order to transfer it you need to have a connection:我用它来检查是否有连接,用它你可以做所有这些,但从这里我将无法将标志转移到 Firebase 我已经失去了连接,因为为了转移它你需要有一个连接:
DatabaseReference connectedRef = FirebaseDatabase.getInstance().getReference(".info/connected");
connectedRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
boolean connected = snapshot.getValue(Boolean.class);
if (connected) {
System.out.println("connected");
} else {
Toast.makeText(DuelGame.this, "У вас отсутствует подключение к интернету!", Toast.LENGTH_SHORT).show();
System.out.println("not connected");
}
}
@Override
public void onCancelled(DatabaseError error) {
System.err.println("Listener was cancelled");
}
});
Once again I will explain more simply.我将再次简单地解释一下。 I need Firebase to check if people are online or not, if not then some actions are enabled.
我需要 Firebase 来检查人们是否在线,如果没有,则启用某些操作。
The code you have allows the client to detect whether it is connected to the Firebase Realtime Database backend.您拥有的代码允许客户端检测它是否连接到 Firebase 实时数据库后端。 As you've discovered, this code does not allow you to update the database once the client disconnects - as at that point there's no way for it to write to the database anymore.
正如您所发现的,一旦客户端断开连接,此代码不允许您更新数据库 - 因为此时它无法再写入数据库。
To detect such disconnect events, Firebase has onDisconnect
handlers .为了检测此类断开连接事件,Firebase 具有
onDisconnect
处理程序。 With this, you register a write operation with the server while you are connection, which the server then executes once it detects that the client is gone.有了这个,你在连接时向服务器注册一个写操作,一旦它检测到客户端已经消失,服务器就会执行这个操作。
The documentation even contains an example of a complete presence system built with .info/connected
and onDisconnect
.该文档甚至包含一个使用
.info/connected
和onDisconnect
构建的完整存在系统的示例。 I recommend using that as the basis for your own use-case too, as it handles some edge-cases you may not immediately think of yourself.我建议也将其用作您自己的用例的基础,因为它可以处理一些您可能不会立即想到自己的边缘情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.