我的firebase连接测试触发连接和断开连接

时间:2021-07-16 22:58:23

I am using the firebase connection connection test detection script to show a modal if the user has disconnected but for some reason when the app loads it fires both cases and it shows the disconnected modal even though it has connection. What am I doing wrong.

我正在使用firebase连接测试检测脚本来显示一个模态,如果用户已经断开连接,但是出于某种原因,当应用程序加载时,它会同时触发这两种情况,并且显示断开连接的模态,即使它有连接。我做错了什么。

var firebaseRef = new Firebase(FIREBAE_URL);
firebaseRef.child('.info/connected').on('value', function(connectedSnap) {
  if (connectedSnap.val() === true) {
    console.log("connected");
  } else {
    console.log("not connected");
    setTimeout(function(){
      $ionicPopup.show({
        title: 'Network is either down or poor wifi',
        template: 'Either pay for some wifi or go to a cafe',
        buttons: [{
          text: '<b>ok</b>',
          type: 'button-positive'
        }]
      });
    }, 8000)

  }
});

2 个解决方案

#1


2  

When you first create a Firebase client, it starts building a connection to the server. This can take some time. Until the connection is fully established, the client is not connected. To it fires false value in .info/connected.

当您第一次创建一个Firebase客户端时,它开始构建到服务器的连接。这可能需要一些时间。直到连接完全建立,客户端才被连接。在.info/connected中触发错误值。

Showing a modal dialog whenever the user loses their connection is probably a bad user experience. Network connections on mobile devices drop surprisingly regularly and Firebase shields the user from brief interruptions effortlessly. Showing a modal dialog is like screaming "YOU'RE OFFLINE, WE'RE ALL DOOMED, YOU HAVE NO INTERNET" in their face. Hardly a pleasant experience.

当用户失去连接时显示一个模态对话框可能是一个糟糕的用户体验。移动设备上的网络连接令人惊讶地下降,而Firebase则可以毫不费力地屏蔽用户的短暂中断。显示一个模态对话就像在他们面前尖叫“你离线了,我们都完蛋了,你没有互联网”。几乎没有一个愉快的经历。

A more typical usage of .info/connected is to show a less obtrusive indicator, such as a little status icon. Alternatively you can show a modal if the user has been offline for a certain period.

info/connected的一个更典型的用法是显示一个不太显眼的指示器,比如一个小的状态图标。如果用户已经离线一段时间,您也可以显示一个模态。

#2


0  

I don't have enough reputation to reply to your comment asking how to avoid the first false event, and I'm not sure if you still want an answer to this, but something simple like this should do the trick:

我没有足够的声誉来回复你关于如何避免第一个错误事件的评论,我也不确定你是否还想要一个答案,但是像这样简单的事情应该可以做到:

var wasPreviouslyConnected = false;
firebaseRef.child('.info/connected').on('value', function(connectedSnap) {
  if (connectedSnap.val() === true) {
    wasPreviouslyConnected = true;
    console.log("connected");
  }
  else if (wasPreviouslyConnected) {
    console.log("disconnected")
  }
  else {
    console.log("initial false connected snapshot while data is still loading");
  }
});

#1


2  

When you first create a Firebase client, it starts building a connection to the server. This can take some time. Until the connection is fully established, the client is not connected. To it fires false value in .info/connected.

当您第一次创建一个Firebase客户端时,它开始构建到服务器的连接。这可能需要一些时间。直到连接完全建立,客户端才被连接。在.info/connected中触发错误值。

Showing a modal dialog whenever the user loses their connection is probably a bad user experience. Network connections on mobile devices drop surprisingly regularly and Firebase shields the user from brief interruptions effortlessly. Showing a modal dialog is like screaming "YOU'RE OFFLINE, WE'RE ALL DOOMED, YOU HAVE NO INTERNET" in their face. Hardly a pleasant experience.

当用户失去连接时显示一个模态对话框可能是一个糟糕的用户体验。移动设备上的网络连接令人惊讶地下降,而Firebase则可以毫不费力地屏蔽用户的短暂中断。显示一个模态对话就像在他们面前尖叫“你离线了,我们都完蛋了,你没有互联网”。几乎没有一个愉快的经历。

A more typical usage of .info/connected is to show a less obtrusive indicator, such as a little status icon. Alternatively you can show a modal if the user has been offline for a certain period.

info/connected的一个更典型的用法是显示一个不太显眼的指示器,比如一个小的状态图标。如果用户已经离线一段时间,您也可以显示一个模态。

#2


0  

I don't have enough reputation to reply to your comment asking how to avoid the first false event, and I'm not sure if you still want an answer to this, but something simple like this should do the trick:

我没有足够的声誉来回复你关于如何避免第一个错误事件的评论,我也不确定你是否还想要一个答案,但是像这样简单的事情应该可以做到:

var wasPreviouslyConnected = false;
firebaseRef.child('.info/connected').on('value', function(connectedSnap) {
  if (connectedSnap.val() === true) {
    wasPreviouslyConnected = true;
    console.log("connected");
  }
  else if (wasPreviouslyConnected) {
    console.log("disconnected")
  }
  else {
    console.log("initial false connected snapshot while data is still loading");
  }
});