Joining an Existing Room

There are several ways to join an existing room.

JoinRandomRoom

The easiest way is to call the JoinRandomRoom method on the StrixNetwork singleton. It has two overloads. The only arguments you have to provide are the properties of the room member that will be created to represent the current client in the room.

One of the overloads accepts the entire RoomMemberProperties object.

void JoinRandomRoom(RoomMemberProperties memberProperties, RoomJoinEventHandler handler, FailureEventHandler failureHandler, RequestConfig config = null)

The other lets you specify only the name of the new room member.

void JoinRandomRoom(string playerName, RoomJoinEventHandler handler, FailureEventHandler failureHandler, RequestConfig config = null)

The downside of this method is that, as can be deduced from the name, the room is selected randomly; you have no way to specify which room you will join. Use JoinRandomRoom is you have only one uniform kind of rooms in your game and the matchmaking is fully random.

JoinRoom

The more general way to join a room is to call JoinRoom on the StrixNetwork singleton.

void JoinRoom(string host, int port, string protocol, long roomId, string playerName, RoomJoinEventHandler handler, FailureEventHandler failureHandler, RequestConfig config = null)

In order to do this, you first need to obtain the ID of the room you’re joining and the address and port of the room server this room exists on. This can be done by searching for the rooms using one of the search functions:

void SearchRoom(int limit, int offset, RoomSearchEventHandler handler, FailureEventHandler failureHandler, RequestConfig config = null)
void SearchRoom(ICondition condition, int limit, int offset, RoomSearchEventHandler handler, FailureEventHandler failureHandler, RequestConfig config = null)
void SearchRoom(ICondition condition, Order order, int limit, int offset, RoomSearchEventHandler handler, FailureEventHandler failureHandler, RequestConfig config = null)
void SearchJoinableRoom(int limit, int offset, RoomSearchEventHandler handler, FailureEventHandler failureHandler, RequestConfig config = null)
void SearchJoinableRoom(ICondition condition, Order order, int limit, int offset, RoomSearchEventHandler handler, FailureEventHandler failureHandler, RequestConfig config = null)

You can read about the search functions in more detail in the Search Rooms section.

When you do a search, you get a list of RoomInfo instances as a result. Each of these instances holds all the information needed for joining the corresponding room as well as additional details such as the room’s properties. The fields you need are the following:

  • host

  • port

  • protocol

  • roomId

There is a more general version of the JoinRoom method as well, that accepts a RoomJoinArgs object. Unlike the others, this one lets you provide a password if the room is password-protected.

void JoinRoom(RoomJoinArgs args, RoomJoinEventHandler handler, FailureEventHandler failureHandler, RequestConfig config = null)

It also lets you provide an external authorization server URL. For more information about this, see the section on Custom Authorization.

Member Properties

Joining a room requires that players identify themselves with member properties.

Required Member Properties:

Name

Type

Description

name

String

The name of the room member

Code Example

public class StrixJoinRoomExample : MonoBehaviour
{
    void Start()
    {
        var strixNetwork = StrixNetwork.instance;

        // This is a placeholder value. Change this to your application ID
        // It can be found on the Strix Cloud application information tab: https://www.strixcloud.net/app/applist
        strixNetwork.applicationId = "00000000-0000-0000-0000-000000000000";

        // First we connect to the master server
        strixNetwork.ConnectMasterServer(
            // This is a placeholder value. Change this to your master hostname
            // It can be found on the Strix Cloud application information tab: https://www.strixcloud.net/app/applist
            host: "000000000000000000000000.game.strixcloud.net",
            connectEventHandler: _ => {
                Debug.Log("Connection established.");

                // After we've connected to the master server we can search for rooms on that server
                strixNetwork.SearchJoinableRoom(
                    condition: null,                                            // Search for all rooms
                    order: new Order("memberCount", OrderType.Ascending),       // Order them in such a way that the first room will be the least crowded
                    limit: 10,                                                  // Only get 10 results
                    offset: 0,                                                  // Starting from the very first one
                    handler: searchResults => {
                        var foundRooms = searchResults.roomInfoCollection;
                        Debug.Log(foundRooms.Count + " rooms found.");

                        // Print the error if no rooms were found
                        if (foundRooms.Count == 0) {
                            Debug.LogError("No joinable rooms found.");
                            return;
                        }

                        // Take the first of the found rooms and join it
                        var roomInfo = foundRooms.First();

                        strixNetwork.JoinRoom(
                            host: roomInfo.host,
                            port: roomInfo.port,
                            protocol: roomInfo.protocol,
                            roomId: roomInfo.roomId,
                            playerName: "My Player Name",
                            handler: __ => Debug.Log("Room joined."),
                            failureHandler: joinError => Debug.LogError("Join failed. Reason: " + joinError.cause)
                        );
                    },
                    failureHandler: searchError => Debug.LogError("Search failed. Reason: " + searchError.cause)
                );
            },
            errorEventHandler: connectError => Debug.LogError("Connection failed. Reason: " + connectError.cause)
        );
    }
}

Note

In order for this example to work, you need to have an active room registered on the master server, but since rooms are destroyed if there are no members left, you would need at least two clients. You can create a match room from another game instance using CreateRoom and then run this example. Otherwise, the search will fail with no available rooms. Also, make sure to change the placeholder values of applicationId and host to the real ones that can be found on the Strix Cloud application information tab.