How to Make Private Room

To create a private room an owner should provide a password. The following code shows how to create such a room using the password property.

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);
    }
);

This code creates the private room. Other members can only join it by providing the correct password as in the following code.

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);
    }
);

Here’s a more complete example of joining the room using the password:

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)
        );
    }
}