简体   繁体   English

如何向数据库发出我失去连接的信号 - Java

[英]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/connectedonDisconnect构建的完整存在系统的示例。 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.

相关问题 Flutter sign in with google lost authentication 当inte.net连接丢失时,如何重新授权? - Flutter sign in with google lost authentication when the internet connection lost, how to re-auth it? 如何让 python 脚本 (abc.py) 在连接丢失或 SSH 连接终止后继续在 AWS 上执行? - How to let the python script (abc.py) keep executing on AWS even after Connection lost or SSH connection is terminated? Github 操作:因“失去连接”而失败 - Github Action : failed with "lost connection" 如何查看Firebase数据库的连接或断开? - How to view connection or disconnection to Firebase database? 如何增加 Google Cloud SQL Postgres 数据库的连接限制? - How to increase the connection limit for the Google Cloud SQL Postgres database? ActiveRecord(无 Rails):如何配置与 azure 数据库的连接 - ActiveRecord (w/o Rails): How to configure a connection to an azure database Firebase函数中如何存储一个sql的数据库连接池? - How to store a sql database connection pool in Firebase functions? 突然丢失了我的 Firebase Firestore 数据库 - Suddenly lost my Firebase Firestore database 如何从 Rust 将 lazy static 连接到 S3? - How to have a connection with lazy static to S3 from Rust? 我正在尝试连接到我创建的大型查询数据库并根据用户输入插入表单数据 - I'm trying to connect to a big query database that I have created and insert form data based on user input
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM