火力地堡配有参与者数量有限 [英] Firebase rooms with limited number of participants

查看:294
本文介绍了火力地堡配有参与者数量有限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

火力地堡,创造房间如聊天很容易,就像记录了他们的各种样品中

In Firebase, creating "rooms" such as for chats is easy, just as documented in their various samples.

有关聊天的数据结构,我会用这样的:

For the data structure of the chat, I would use something like this:

rooms
    room1
        member_count
        members
            user1
            user2
        messages
            message1

但现在我想创建创建的每间客房参与者的数量限制,比如3每用户聊天室。

But now I would like to create to create a limit on the number of participants per room, say 3 users per chat room.

你怎么能这样做呢?

在他们的文档,一件事,看上去最有前途的是用交易的。您可以验证这是一个很好的路要走?或者这是错误的做法?

In their docs, the one thing that looked most promising was using transactions. Can you verify that this is a good way to go? Or is this the wrong approach?

关于这样一个解决方案是什么?

What about a solution like this?

Firebase countRef = new Firebase("https://mychat.firebaseIO-demo.com/rooms/room1");
countRef.runTransaction(new Transaction.Handler() {
    @Override
    public Transaction.Result doTransaction(MutableData currentData) {
        int oldMemberCount = currentData.child("member_count").getValue(Integer.class);
        currentData.child("member_count").setValue(oldMemberCount + 1); // try to update member count
        return Transaction.success(currentData);
    }

    @Override
    public void onComplete(FirebaseError error, boolean committed, DataSnapshot currentData) {
        if (error != null || !commited) {
            // rollback value (how? just do nothing?)
        }
        else {
            // transaction has been commited (value has already been saved?)
            currentData.child("members").child(CURRENT_USER_UUID).setValue(CURRENT_USER_NAME); // add user to the members list
        }
    }
});

这将是巨大的,如果你能对这种做法发表评论。此外,如果事务已失败人们不能在当然这种情况满意。用户仍然希望加入,不管有另一用户试图在同一时间加入。那么是什么做的?在错误的情况下再次将这个code到函数,并调用功能?

It would be great if you could comment on this approach. Furthermore, one cannot be satisfied in that situation of course if the transaction has failed. The user still wants to join, no matter that there was another user trying to join at the same time. So what do to? Put this code into a function and call the function again in the error case?

编辑:

要创建一个新的房间与自动唯一的ID,人们当然可以使用推()火力地堡引用。

To create a new room with automatically unique ID, one could certainly use push() on a Firebase reference.

但是,如果你想将成员添加到那个房间的话,上述问题仍然存在。一个替代方案可以是,设置接合在用户的优先中的成员列表。当他们的优先级设置为当前的时间戳,我们可以再额度成员列表回调至3 (成员)。但是,这似乎并不优雅,也不干净。

But if you want to add members to that room then, the problem described above remains. An alternative solution could be to set users' priority in the member list when joining. When setting their priority to the current timestamp, one could then limit the member list callbacks to 3 (members). But that doesn't seem to be elegant nor clean.

推荐答案

如果你有一个固定的(和相对较小),每间客房参与人数,这将会是最好用的交易。然而,它可能是最好在聊天室来为每个人公命名的对象,例如:

If you have a fixed (and relatively small) number of participants per room, it'd be best to use transactions. However, it might be best to create well-named objects for each person in a chat room, for example:

/rooms
  /<roomid, generated by push()>
    /users
      one: null
      two: null
      three: null

加入一个房间看起来像(在JavaScript code,请转换成Java如适用);

Joining a room would look like (code in JavaScript, please convert to Java as appropriate);

var userid = "myuserid";
var ref = new Firebase("<my-firebase>.firebaseio.com/rooms/<roomid>/users");
ref.transaction(function(users) {
  if (!users.one) {
    // Claim slot 1
    users.one = userid;
    return users;
  } else if (!users.two) {
    // Claim slot 2
    users.two = userid;
    return users;
  } else if (!users.three) {
    // Claim slot 3
    users.three = userid;
    return users;
  }
  // Room is full, abort the transaction.
  return;
}, function(err, committed, snapshot) {
  if (committed && !err) {
    // Joined room successfully.
  } else {
    // Could not join room because it was full.
  }
});

如果未能提交值到服务器

火力地堡将自动调用交易功能。除了上面的code,你还需要实施prevent用户声称这已经采取了一些插槽安全规则:

Firebase will automatically call the transaction function if it fails to commit the value to the server. In addition to the code above, you'll also need to implement some security rules that prevent users from claiming a slot that's already taken:

{
  "rules": {
    "rooms": {
      "$roomid": {
        "users": {
          "$slot": {
            ".write": "!data.exists()"
          }
        }
      }
    }
  }
}

您可以上传通过锻造,为你的火力地堡图形调试这些规则,你应该是好去!

You can upload these rules via Forge, the graphical debugger for your Firebase and you should be good to go!

这篇关于火力地堡配有参与者数量有限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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