ここでは例を使ってルームに参加した際にルームのプロパティを同期させる方法を説明します。
新しいメンバーが Room に参加する場合、新メンバーのクライアントは、数値プロパティの現在の値を取得する必要があります。
下のコード例の流れは以下になります:
- Room Join → OnJoin イベントが発生
- OnJoin イベントの処理の中で GetProperties を呼び出す → OnGetProperties イベントが発生
- OnGetProperties イベントの中で同期の処理をする
// We retrieve "HP" as we join a room
room.OnJoin += OnDiarkisRoomJoin;
private void OnDiarkisRoomJoin(bool success, uint createdTime)
{
if (!success)
{
// Handle error...
return;
}
// Retrieve "HP"
List<string> propertyNames = new List<string>();
propertyNames.Add("HP");
// This raises OnGetProperties event and that is how you retrieve the property data
room.GetProperties(room.GetRoomID(), propertyNames);
}
// This is how to retrieve "HP" from the server
room.OnGetProperties += OnDiarkisRoomGetProperties;
private void OnDiarkisRoomGetProperties(bool success, Dictionary<string, byte[]>properties)
{
if (!success)
{
// Handle error...
return;
}
if (!properties.ContaionsKey("HP"))
{
// "HP" is missing...
return;
}
// Update local "HP"
Array.Reverse(properties["HP"]); // Big Endian
_hp = BitConverter.ToInt64(msg.Value, 0);
}