【问题标题】:Firebase Listener does not identify or resume connection after idle timeFirebase 侦听器在空闲时间后无法识别或恢复连接
【发布时间】:2018-04-26 04:18:05
【问题描述】:

我的项目是识别客户端是在线还是离线。我使用 Android-Firebase 文档中提供的代码,它在“.info / connected”中使用 EventListener。

// since I can connect from multiple devices, we store each connection instance separately
// any time that connectionsRef's value is null (i.e. has no children) I am offline
final FirebaseDatabase database = FirebaseDatabase.getInstance();
final DatabaseReference myConnectionsRef = database.getReference("users/joe/connections");

// stores the timestamp of my last disconnect (the last time I was seen online)
final DatabaseReference lastOnlineRef = database.getReference("/users/joe/lastOnline");

final DatabaseReference connectedRef = database.getReference(".info/connected");
connectedRef.addValueEventListener(new ValueEventListener() {
  @Override
  public void onDataChange(DataSnapshot snapshot) {
    boolean connected = snapshot.getValue(Boolean.class);
    if (connected) {
      DatabaseReference con = myConnectionsRef.push();

      // when this device disconnects, remove it
      con.onDisconnect().removeValue();

      // when I disconnect, update the last time I was seen online
      lastOnlineRef.onDisconnect().setValue(ServerValue.TIMESTAMP);

      // add this device to my connections list
      // this value could contain info about the device or a timestamp too
      con.setValue(Boolean.TRUE);
    }
  }

  @Override
  public void onCancelled(DatabaseError error) {
    System.err.println("Listener was cancelled at .info/connected");
  }
});

此方法可以正确识别用户何时终止或重新打开应用程序,或者何时在 Internet 上发生轻微断开连接。但是,我做了一个测试,关闭设备的 Wifi 2 分钟,在 Firebase 数据库中识别为未连接,但是当我打开 Wifi,恢复互联网连接时,Firebase 无法识别活动连接,看起来好像用户仍然处于非活动状态。

这段代码 sn-p 被插入到我的 MainActivity 的 onCreate 方法中。

谁能告诉我为什么会这样?


编辑: 附件

错误日志:

11-14 10:12:55.272 27901-27926/com.example.davi.projetofirebase D/PersistentConnection: pc_0 - 尝试获取身份验证令牌

11-14 10:12:55.276 27901-27926/com.example.davi.projetofirebase D/PersistentConnection: pc_0 - 成功获取令牌,打开连接

11-14 10:12:55.281 27901-27926/com.example.davi.projetofirebase D/Connection: conn_14 - 打开连接

11-14 10:12:55.293 27901-27926/com.example.davi.projetofirebase D/WebSocket:ws_14 - WebSocket 错误。 bkx:未知主机:s-usc1c-nss-208.firebaseio.com 在 bku.d(:com.google.android.gms.DynamiteModulesC@11746448:102) 在 bkv.run(:com.google.android.gms.DynamiteModulesC@11746448:4) 在 java.lang.Thread.run(Thread.java:760) 引起:java.net.UnknownHostException:s-usc1c-nss-208.firebaseio.com 在 java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:174) 在 java.net.SocksSocketImpl.connect(SocksSocketImpl.java:356) 在 java.net.Socket.connect(Socket.java:586) 在 com.android.org.conscrypt.OpenSSLSocketImpl.connect(OpenSSLSocketImpl.java:265) 在 com.android.org.conscrypt.OpenSSLSocketImpl.connect(OpenSSLSocketImpl.java:252) 在 java.net.Socket.(Socket.java:427) 在 java.net.Socket.(Socket.java:210) 在 javax.net.ssl.SSLSocket.(SSLSocket.java:906) 在 com.android.org.conscrypt.OpenSSLSocketImpl.(OpenSSLSocketImpl.java:192) 在 com.android.org.conscrypt.OpenSSLSocketFactoryImpl.createSocket(OpenSSLSocketFactoryImpl.java:68) 在 bku.d(:com.google.android.gms.DynamiteModulesC@11746448:94) 在 bkv.run(:com.google.android.gms.DynamiteModulesC@11746448:4) 在 java.lang.Thread.run(Thread.java:760)

11-14 10:12:55.294 27901-27926/com.example.davi.projetofirebase D/WebSocket: ws_14 - 自行关闭

11-14 10:12:55.295 27901-27926/com.example.davi.projetofirebase D/Connection: conn_14 - 实时连接失败

11-14 10:12:55.295 27901-27926/com.example.davi.projetofirebase D/Connection: conn_14 - 关闭实时连接

11-14 10:12:55.295 27901-27926/com.example.davi.projetofirebase D/PersistentConnection: pc_0 - 由于 OTHER 断开连接

11-14 10:12:55.296 27901-27926/com.example.davi.projetofirebase D/PersistentConnection: pc_0 - 计划连接尝试

11-14 10:12:55.297 27901-27926/com.example.davi.projetofirebase D/ConnectionRetryHelper:在 27403 毫秒内安排重试

11-14 10:12:55.304 27901-27926/com.example.davi.projetofirebase D/WebSocket: ws_14 - 关闭

11-14 10:13:02.947 27901-27926/com.example.davi.projetofirebase D/PersistentConnection:pc_0 - 连接中断:connection_idle

11-14 10:13:02.960 27901-27926/com.example.davi.projetofirebase D/ConnectionRetryHelper:取消现有的重试尝试

【问题讨论】:

  • 当您说“Firebase 无法识别活动连接”时,您是如何注意到的?该用户的控制台中的节点是否未更新?还是客户没有获得新的价值?另外:如果你enable debug logging为客户端,你应该能够看到客户端是否正在重新建立它的连接。
  • @FrankvanPuffelen 综上所述:开启Wifi后,表示已建立连接的节点并未在数据库中创建(users/joe/connections)。此外,Listener 不再触发 onDataChange 方法。我会尝试启用调试,看看会发生什么。
  • @FrankvanPuffelen 请查看我添加到问题描述中的附件
  • 嗯....看来您的客户端根本无法重新连接,这意味着在线状态系统本身工作正常(.info/connected 应该只有 true 当你连接的)。我刚刚尝试重现此内容,但在我的系统上,当我恢复连接时,客户端会立即重新连接。您使用的是什么 SDK 版本?
  • compileSdkVersion: 26, targetSdkVersion: 26, Device SDK Version: 24, Firebase Version: 'com.google.firebase:firebase-storage:11.0.4', Tool: Android Studio 3.0, 是什么原因问题可能是因为我没有使用 Firebase Auth 对客户端进行身份验证?

标签: android firebase firebase-realtime-database


【解决方案1】:

我联系了 Firebase 支持团队,他们告诉我一个显然解决了我的问题的解决方案:

如果数据库没有活动的操作或侦听器,显然 .info / connected 的侦听器将停止侦听。我向我数据库中的任何一个节点添加了另一个帮助侦听器,并且它起作用了!

DatabaseReference myConnectionRef2 = database.getReference("/users/joe/lastOnline");
myConnectionRef2.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if (dataSnapshot.getValue() == null) {
            System.out.println("FIREBASE: Aux connected");
        } else {
            System.out.println("FIREBASE: Aux not connected");
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

每次我失去网络连接时,两个监听器都会尝试恢复通信,直到网络重新连接。

感谢所有帮助我解决这个问题的人。

【讨论】:

    【解决方案2】:

    正如弗兰克斯所说,如果您为客户端启用debug logging,无论客户端是否正在重新建立其连接,都可以确保看到连接状态。

    public synchronized void setLogLevel (Logger.Level logLevel)
    

    默认设置为INFO。这包括客户端收到的任何内部错误 (ERROR) 和任何安全调试消息 (INFO)。设置为 DEBUG 以打开诊断日志记录,设置为 NONE 以禁用所有日志记录。

    【讨论】:

    • 客户端的连接状态不应该被持久化到磁盘。我认为磁盘持久性与 Davi 询问的 .info/connected 节点无关。
    • @FrankvanPuffelen 谢谢!你说的对。刚刚更新了我的答案。
    • @AlexMamo 感谢您的帮助。我会尝试启用调试,看看会发生什么。
    • 是的,试一试。也让我知道。
    • @AlexMamo 请查看我添加到问题描述中的附件。
    猜你喜欢
    • 2017-10-27
    • 1970-01-01
    • 1970-01-01
    • 2016-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-20
    • 2012-04-02
    相关资源
    最近更新 更多