Skip to main content

Matchmaking

With the Matchmaking API you are able to create custom game sessions that can be searched by your users based on key-value pairs that you can define. You can start implementing it by using the following namespace and class.

namespace PretiaArCloud.Multiplayer.Matchmaking
public class Matchmaker

Methods

CreateSessionAsync


Description

Whenever you want to build your own game sessions you can use CreateSessionAsync to accomplish this. The result of this function can be later used to join the session.

public async Task<(SessionInfo, IPEndPoint)> CreateSessionAsync(
string token,
string sessionName,
bool priv,
IDictionary<string, string> labels)

Parameters

string token

Token obtained from GuestLoginAsync.

string sessionName

Name of the session you want to create.

bool priv

Sets whether the created session is private or not.

IDictionary<string, string> labels

key-value pair list of labels that can be used to filter the created session.

GetSessionInfoAsync


Description

You can use GetSessionInfoAsync to find created sessions that match the labels provided as parameter.

public async Task<IEnumerable<SessionInfo>> GetSessionInfoAsync(
string token,
IDictionary<string, string> labelFilters)

Parameters

string token

Token obtained from GuestLoginAsync.

IDictionary<string, string> labels

key-value pair list of labels. If this labels match a create session this will be returned in the list of results.

GetMatchIPAsync


Description

You can obtain the IP:Port pair of a session if you have a SessionInfo.

public async Task<IPEndPoint> GetMatchIPAsync(string token, SessionInfo sessionInfo)

Parameters

string token

Token obtained from GuestLoginAsync.

SessionInfo sessionInfo

SessionInfo object obtained either from CreateSessionAsync or GetSessionInfoAsync

Sample Code

In order to successfuly utilize the Matchmaking API we will need to follow this flow :

  1. Get a valid token
  2. Create a new session / find available sessions
  3. Join session

Step 1 : Get a token

if (await _networkManager.ConnectAsync())
{
string guest = "guestName";
var (statusCode, token, displayName) = await _networkManager.GuestLoginAsync(guest);

if (statusCode == NetworkStatusCode.Success)
{
// Do something with the token from step 2A or 2B
}
}

Step 2A : Create a Game Session using obtained token

var sessionName = "session-name";
var labels = new Dictionary<string, string>();
labels.Add("my-key", "my-value"); // Add a key-value string pair for the session label

var (sessionInfo, ipEndPoint) = Matchmaker.Instance.CreateSessionAsync(token, sessionName, priv: false, labels);

Step 2B : Find available sessions on a different client and get its IP Endpoint

var labels = new Dictionary<string, string>();
labels.Add("my-key", "my-value"); // Add a key-value string pair to match sessions

var sessions = await matchmaker.GetSessionInfoAsync(token, labels); // Returns all available sessions with exact match of labels
var sessionInfo = sessions.FirstOrDefault() // Get the first item in sessions
var ipEndPoint = await Matchmaker.Instance.GetMatchIPAsync(token, sessionInfo); // Get the IP Endpoint of the session

Step 3 : Connect to the game session with its IP Endpoint

var gameSession = await NetworkManager.Instance.CreateGameSessionAsync(ipEndPoint, token);
await gameSession.ConnectSessionAsync();

Further details


This was an illustrative example on how to use the Matchmaking API. For a complete example refer to our sample scene located in /PretiaSDK/Samples/MultiplayerSample/Scenes/MultiplayerScene.