Firebase持久性-onDisconnect与多个浏览器窗口 [英] Firebase persistance - onDisconnect with multiple browser windows

查看:92
本文介绍了Firebase持久性-onDisconnect与多个浏览器窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在编写一个监视在线状态的应用程序.在多种情况下,我们需要用户打开多个浏览器窗口.我们遇到了一个问题,即用户在辅助浏览器窗口中打开并运行firebase js代码后,将关闭该辅助窗口.这会在主窗口中将其状态设置为脱机,因为onDisconnect事件在辅助窗口中触发.

We're writing an app that monitors online presence. There are multiple scenarios where we need the user to have more than one browser window open. We're running into a problem where the user, after opening and running the firebase js code in a secondary browser window, will close that secondary window. This sets their presence to offline in the primary window because the onDisconnect event fires in the secondary window.

此方案是否有解决方法?这是可以在/.info/connected特殊位置使用的位置吗?

Is there a workaround for this scenario? Is this where the special /.info/connected location could be used?

推荐答案

.info/connected状态数据仅告诉给定的客户端是否已将其链接到Firebase服务器,因此在这种情况下它不会为您提供帮助.您可以尝试以下方法之一:

The .info/connected presence data only tells a given client if they are linked up to the Firebase server, so it won't help you in this case. You could try one of these:

如果变量离线则重置该变量

如果您有多个客户端监视同一个变量(多个窗口属于此类别),则很自然地希望它们与状态值发生冲突.让每个人监视变量的更改并在必要时进行更正.

If you have multiple clients monitoring the same variable (multiple windows falls into this category), it's natural to expect them to conflict about presence values. Have each one monitor the variable for changes and correct it as necessary.

var ref = firebaseRef.child(MY_ID).child('status');
ref.onDisconnect().remove();
ref.on('value', function(ss) {
   if( ss.val() !== 'online' ) {
      // another window went offline, so mark me still online
      ss.ref().set('online');
   }
});

这是快速且易于实现的操作,但可能会令人讨厌,因为我的状态可能会忽悠到离线,然后又为其他客户重新联机.当然,可以通过在触发UI更新之前对任何更改事件稍加延迟来解决该问题.

This is fast and easy to implement, but may be annoying since my status might flicker to offline and then back online for other clients. That could, of course, be solved by putting a short delay on any change event before it triggers a UI update.

为每个连接赋予自己的路径

由于onDisconnect的目的是告诉您特定的客户端是否离线,所以我们自然应该给每个客户端自己的连接:

Since the purpose of the onDisconnect is to tell you if a particular client goes offline, then we should naturally give each client its own connection:

var ref = firebaseRef.child(MY_ID).child('status').push(1);
ref.onDisconnect().remove();

在此用例中,每个连接的客户端在status下添加一个新的子代.如果路径不为null,则说明至少有一个客户端连接.

With this use case, each client that connects adds a new child under status. If the path is not null, then there is at least one client connected.

通过在status上查找真实值来检查在线状态实际上非常简单:

It's actually fairly simple to check for online presence by just looking for a truthy value at status:

firebaseRef.child(USER_ID).child('status').on('value', function(ss) {
    var isOnline = ss.val() !== null;
});

这的一个缺点是您确实只有status一个通俗的值(您不能将其设置为"idle"和"online"之类的值),尽管这也可以通过一点点解决独创性.

A downside of this is that you really only have a thruthy value for status (you can't set it to something like "idle" vs "online"), although this too could be worked around with just a little ingenuity.

在这种情况下交易无效

我的第一个想法是尝试进行事务处理,因此可以在打开/关闭每个窗口时增加/减少计数器,但是在onDisconnect事件中似乎没有transaction方法.所以这就是我想出的多路径状态值.

My first thought was to try a transaction, so you could increment/decrement a counter when each window is opened/closed, but there doesn't seem to be a transaction method off the onDisconnect event. So that's how I came up with the multi-path presence value.

这篇关于Firebase持久性-onDisconnect与多个浏览器窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆