客户端JavaScript中多个Firebase连接的资源限制 [英] Resource limits around multiple Firebase connections in client-side JavaScript

查看:113
本文介绍了客户端JavaScript中多个Firebase连接的资源限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我想制作一个完全在浏览器中运行的大规模,实时的2D开放式游戏,我想用Firebase来做。



<让我们也留下关于安全的问题。我会问以后。 (0,0),可以向任意方向移动,我在空间上哈希世界,使任何给定的x,y坐标变成一个单一的(或者,嵌套),我可以使用它来生成一个Firebase ref:

  var getKey = function(x,y){
返回Math.floor(x / 100)+':'+ Math.floor(y / 100);
}

var key = getKey(currX,currY);

var ref = new Firebase('https://whatever.firebaseio.com/world/'+ key);
// ...

类似这样的情况,当玩家在世界各处移动时,我很确定我需要保留四至九位参与Firebase活动的地方取决于我如何构建数据,这可能会增加一倍或三倍:其他玩家存储在 world / 树或其他地方吗? / p>

我不希望玩家的浏览器得到不关心的更新,我想在玩家移动时过期所以浏览器不会花费资源聊天世界的远方片。



允许ref实例超出范围并得到GCed足够了吗?或者还有什么我不得不做的,以通知裁判员,我不想再使用它?



我想到的另一件事是不担心有多少裁判,而是跟踪回调上。当玩家离开世界某个特定区域时,我可以 off 我设定的任何回调。 Firebase只保留一个打开的连接(使用网络套接字),这将足以让我的文档停止与Firebase的对话?

处理与单个Firebase实例的通信,即使您为其创建了多个参考。所以就像你说的那样 - 只要你在不活动的参考文献中关闭了任何事件,就应该是好的。

您可能希望考虑在启用Firebase调试日志发展。它会让你看到浏览器和服务器之间发生的所有通信。这样,您将能够验证客户端只获取必要的更新。要启用调试日志记录,请在创建第一个Firebase引用之前的任意位置添加以下行:

  Firebase.enableLogging(true); 


Let's say that I wanted to make a massive, realtime, 2d open-world style game that runs entirely in the browser and I wanted to use Firebase to do it.

Let's also leave aside questions about security; I'll ask those later. ( =

Players start at (0,0) and can move in any direction. I spatially hash the world so that any given x,y coordinate becomes a single (or nested) key that I can use to generate a Firebase ref:

var getKey = function(x, y) {
  return Math.floor(x / 100) + ':' + Math.floor(y / 100);
}

var key = getKey(currX, currY);

var ref = new Firebase('https://whatever.firebaseio.com/world/' + key);
// ...

Something like that. As the player moves around in the world I'm pretty sure I'd need to keep somewhere between four and nine refs to Firebase alive to get changes. This could double or triple depending on how I structure the data, too: are other players stored in the world/ tree, or elsewhere? That kind of thing.

I don't want the player's browser to get updates it doesn't care about. I'd like to "expire" old refs as the player moves around so the browser isn't spending resources chatting about faraway slices of the world.

Is it enough to let the ref instance go out of scope and get GCed? Or is there something else I'd have to do to notify the ref that I don't want to use it anymore?

Another thing I thought about was not worrying about how many refs there are but tracking on callbacks instead. As the player moves out of the range of a particular section of the world I could off any callbacks I'd set up. Would that be sufficient to get my refs to stop talking to Firebase?

解决方案

Firebase keeps only one open connection (using web sockets) to handle the communication to a single Firebase instance, even if you create multiple refs for it. So just like you said - as long as you 'off' any events on the inactive refs, you should be good.

You may wish to consider enabling Firebase debug logging during development. It will let you see all the communication that takes place between the browser and their server. This way, you will be able to verify that the client is getting only the necessary updates. To enable debug logging, add the following line anywhere before you create the first Firebase ref:

Firebase.enableLogging(true);

这篇关于客户端JavaScript中多个Firebase连接的资源限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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