2

一个基于 firebase 的游戏,每个用户在同一个 waitingList 上推送一个游戏请求,以便 nodeJS 服务器分别为每两个等待的玩家准备一个回合游戏。我在 nodeJS 上使用 Firebase JS v3:

    var ref = firebase.database().ref();

//this is where we populate rounds
var gamesRef = ref.child('games');

//users path
var usersRef = ref.child('users');

//waiting queue
var waitingRef = ref.child('waitingList');

//listen for each new queued user
waitingRef.on('child_added', function(snap){ 
  waitingRef.transaction(function(waitingList){
    if(waitingList !== null){
      var keys = Object.keys(waitingList);
      //take two players
      if(keys.length >= 2){
        //prepare a new round for users
        var round = prepareNewRound(waitingList[keys[0]], waitingList[keys[1]]);

        //remove queued users from waitingList
        waitingList.child(keys[0]).remove();
        waitingList.child(keys[1]).remove();

        //create new game
        var roundKey = gamesRef.push(round).key;

        //notify users about the new game created
        users.child(waitingList[keys[0]]).child('current_round').set(roundKey);
        users.child(waitingList[keys[1]]).child('current_round').set(roundKey);
      }
    }
  });
});

可能超过 10,000 个同时实时用户的用户强烈要求等待列表。这种方式的执行速度比 Firebase-queue 快,但每 5 分钟重复慢 1 分钟,等待列表变得越来越长,之后才变得正常。是否可以对该概念进行任何优化?

4

0 回答 0