비공개 방 만드는 방법¶
비공개 방을 만들려면 방장이 비밀번호를 제공해야 합니다. 다음은 비밀번호 속성을 이용하여 방을 만드는 방법을 보여주는 코드입니다.
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)
);
}
}