Room を用いたマルチプレーヤーアプリを開発しているときに、クライアント側では「いま参加している Room から別の Room へ移動させる」ということをしたくなることはよくあります。
Diarkis の Room モジュールでは、この操作をシンプルに実装するための Move メソッドを提供しています。
このエントリで紹介する Move メソッドでは、イベントリスナーもしくはコールバックを利用します。
イベントリスナーで実装する
room.OnLeave += HandleDiarkisRoomOnLeave;
room.OnJoin += HandleDiarkisRoomOnJoin;
// leaveMessage will be broadcasted when leaving the current room - If you pass an empty byte array, the message will not be sent
byte[] leaveMessage = Encoding.UTF8.GetBytes("Good bye");
// joinMessage will be broadcasted when joining a new room - If you pass an empty byte array, the message will not be sent
byte[] joinMessage = Encoding.UTF8.GetBytes("Hello world");
// roomID is the new room ID to join
room.Move(roomID, leaveMessage, joinMessage);
private void HandleDiarkisRoomOnLeave(bool success)
{
if (!success)
{
// Failed to leave the current room
}
}
private void HandleDiarkisRoomOnJoin(bool success, uint roomCreatedTimeInMilliseconds)
{
if (!success)
{
// Failed to join the new room
}
}
コールバックで実装する
// leaveMessage will be broadcasted when leaving the current room - If you pass an empty byte array, the message will not be sent
byte[] leaveMessage = Encoding.UTF8.GetBytes("Good bye");
// joinMessage will be broadcasted when joining a new room - If you pass an empty byte array, the message will not be sent
byte[] joinMessage = Encoding.UTF8.GetBytes("Hello world");
// roomID is the new room ID to join
room.Move(roomID, leaveMessage, joinMessage, OnMoveCallback);
private void OnMoveCallback(bool success, uint roomCreatedTimeInMilliseconds)
{
if (!success)
{
// Handle error here...
}
// Do something awesome here!
}
※ C# client v0.1.14 以上で動作します。
おわりに
Diarkis の Room 利用中に、他の Room へ移動したくなったときは Move メソッドを使うことでシンプルに Room 移動を実現できます。
Move はイベントリスナーを設定するか、コールバックによって実装することができます。