Detect if Firebase connection is lost/regained

前端 未结 6 881
[愿得一人]
[愿得一人] 2020-11-27 12:25

Is there a strategy that would work within the current Firebase offering to detect if the server connection is lost and/or regained?

I\'m considering some offline co

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-27 12:40

    The suggested solution didn't work for me, so I decided to check the connection by writing and reading 'health/check' value. This is the code:

    const config = {databaseURL: `https://${projectName.trim()}.firebaseio.com/`};
    //if app was already initialised delete it
    if (firebase.apps.length) {
        await firebase.app().delete();
    }
    // initialise app
    let cloud = firebase.initializeApp(config).database();
    // checking connection with the app/database
    let connectionRef = cloud.ref('health');
    connectionRef.set('check')
        .then(() => {
            return connectionRef.once("value");
        })
        .then(async (snap) => {
            if (snap.val() === 'check') {
                // clear the check input
                await connectionRef.remove();
                // do smth here becasue it works
        }
    });
    

提交回复
热议问题