3

私はfirebaseを使用してチャットでリアルタイムの仮想ルームに取り組んでいます。firebase db の特定のフィールドの更新をリッスンできるかどうか疑問に思っていました。たとえば、データが次のように構造化されている場合:

{channel_name: test-app,
  {id: unique_id_generated_automatically_by_firebase,
    {user_id: some_id,
     position: {
       current: {x:x,y:y},
       start: {x:x,y:y},
       end: {x:x,y:y}
    }
  }
  {id: unique_id_generated_automatically_by_firebase,
    {user_id: some_id,
     position: {
       current: {x:x,y:y},
       start: {x:x,y:y},
       end: {x:x,y:y}
    }
  }
} 

現時点では、そのようなデータベースの変更をリッスンできます

//reference to firebase db
var room = new Firebase("https://test-app.firebaseio.com/");
room.on("child_changed", function(snapshot) {
   //do something here; 
});

私が探しているのは、フィールド position.start および position.end の変更をリッスンする方法ですが、position.current は無視します (これらは更新される唯一のフィールドです)。現在の位置は、現在ルームにいるすべてのユーザーの現在の位置を取得するためにユーザーがログインする場合にのみ必要になります。その後、位置は開始値と終了値に基づいてクライアント マシン上でアニメーション化されます。また、接続されているすべてのクライアントに現在の位置の変更を発行せずに、要求されたときに現在の状態を取得することで、データ転送を節約したいと考えています。どんな助けや提案も大歓迎です。

4

1 に答える 1

2

関心のあるフィールドごとに .on("value") イベントをバインドできます。

var room = new Firebase("https://test-app.firebaseio.com/" + roomID);
room.child("position/start").on("value", onChange);
room.child("position/end").on("value", onChange);
function onChange(snapshot) {
  if (snapshot.name() == "start") {
    // position.start changed to snapshot.val()
  } else if (snapshot.name() == "end") {
    // position.end changed to snapshot.val()
  }
}
于 2013-09-18T18:28:15.007 に答える