Detect if Firebase connection is lost/regained

前端 未结 6 880
[愿得一人]
[愿得一人] 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:52

    I guess this changed in the last couple of months. Currently the instructions are here: https://firebase.google.com/docs/database/web/offline-capabilities

    In summation:

    var presenceRef = firebase.database().ref("disconnectmessage");
    // Write a string when this client loses connection
    presenceRef.onDisconnect().set("I disconnected!");
    

    and:

    var connectedRef = firebase.database().ref(".info/connected");
    connectedRef.on("value", function(snap) {
      if (snap.val() === true) {
        alert("connected");
      } else {
        alert("not connected");
      }
    });
    

    I'll admit I don't know a lot about how references are set, or what that means (are you making them out of thin air or do you have to have already created them beforehand?) or which one of those would trigger something on the server as opposed to something on the front end, but if the link is still current when you read this, a little more reading might help.

提交回复
热议问题