プライベートルームの作り方

プライベートルームを作成するには、オーナーがパスワードを設定する必要があります。次のコードは、passwordプロパティを使用してそのようなルームを作成する方法を示しています。

StrixNetwork.instance.CreateRoom(
    new RoomProperties {
        name = "New Room",
        capacity = 4,
        password = "Room password"
    },
    new RoomMemberProperties {
        name = "Player name"
    },
    args => {
        Debug.Log("CreateRoom succeeded");
    },
    args => {
        Debug.Log("CreateRoom failed. error = " + args.cause);
    }
);

このコードは、プライベートルームを作成します。他のメンバーは、次のコードのように正しいパスワードを入力することによってのみ参加できます。

StrixNetwork.instance.JoinRoom(
    new RoomJoinArgs {
        host = "127.0.0.1",
        port = 9123,
        protocol = "TCP",
        roomId = 1,
        password = "Room password"
    },
    args => {
        Debug.Log("JoinRoom succeeded");
    },
    args => {
        Debug.Log("JoinRoom failed. error = " + args.cause);
    }
);

これは、パスワードを使用してルームに参加する、より完全な例です。

using SoftGear.Strix.Client.Core.Model.Manager.Filter;
using SoftGear.Strix.Unity.Runtime;
using System;
using System.Linq;
using UnityEngine;

public class PasswordProtectedRoomSample
{
    public static void TryToJoinRoom(ICondition condition, RoomMemberProperties memberProperties, string password, Action onRoomEntered)
    {
        StrixNetwork.instance.SearchJoinableRoom(condition, null, 10, 0,
            searchResult => {
                var roomInfo = searchResult.roomInfoCollection.FirstOrDefault();

                if (roomInfo != null) {
                    StrixNetwork.instance.JoinRoom(
                        args: new RoomJoinArgs {
                            host = roomInfo.host,
                            port = roomInfo.port,
                            password = password,
                            memberProperties = memberProperties
                        },
                        handler: joinResult => onRoomEntered.Invoke(),
                        failureHandler: joinError => CreateRoom(memberProperties, password, onRoomEntered)
                    );
                } else {
                    CreateRoom(memberProperties, password, onRoomEntered);
                }
            },
            searchError => CreateRoom(memberProperties, password, onRoomEntered)
        );
    }

    private static void CreateRoom(RoomMemberProperties memberProperties, string password, Action onRoomEntered)
    {
        StrixNetwork.instance.CreateRoom(
            new RoomProperties {
                password = password
            },
            memberProperties,
            createResult => onRoomEntered.Invoke(),
            createError => Debug.LogError(createError.cause)
        );
    }
}