株式会社Diarkis

トップ > 技術ブログ > ある Room から別の Room へ移動する方法

ある Room から別の Room へ移動する方法

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 はイベントリスナーを設定するか、コールバックによって実装することができます。

大規模マルチユーザー間通信をDiarkisが実現します

お問い合わせ
このエントリーをはてなブックマークに追加

この記事もおすすめ

Diarkisをもっと知りたい、採用を検討したい場合は以下よりお問い合わせください

お問い合わせ 資料請求
PAGE TOP