How to signal to the database that I have lost the connection - Java

SuperMen

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

image

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:

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.

Frank van Puffelen

The code you have allows the client to detect whether it is connected to the Firebase Realtime Database backend. 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. 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. 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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to simulate lost database connection locally?

Swift: How can I have a listener that reports when connection is lost and when it comes back?

I have lost my ethernet connection since resetting powerline plugs

How to catch exception when database connection is lost when using EF?

Java detect lost connection

Have I lost a commit?

How do I create a BLOB object by Java without connection to database

Should I have a single database connection or one connection per task?

How to connect to database connection in Java

How to re-establish lost KDB connection in Java

Error: "I/Process (26960): Sending signal. PID: 26960 SIG: 9 Lost connection to device." while using flutter on android studio

How can I do Qt QLed signal slot connection?

Partition table lost - how to recover lost LUKS partition, I have some backup partitioning data

Force django to reopen database connection if lost

Restore NHibernate after lost Oracle database connection

PHP daemon parent database connection lost

When do I have to close mysqli (Database) connection?

how to detect connection lost in vlcj

how to deal with socketexcrption connection lost

How to play a Voiceover notification when signal lost (i.e., no user interaction)?

is it fine to have normal java class to handle database connection with servlet?

How do I incorporate database connection into AspectJ?

How can I close the database connection in this approach?

How reconnect and retry a MySQL query in PHP when the database connection has been lost?

How to have a database connection without failing unit tests with Spring Boot?

How to test connection to Oracle Database using Java

How can I know if the plc connection is lost once I subscribed a tag?

How should I use sqlalchemy session in bottle app to avoid 'Lost connection to MySQL server during query'

Have I lost my changes after rebasing?