Match Up
Provides simple drop-in matchmaking support for any networking system.
Matchmaker Class Reference

Provides functionality for creating, listing, joining, and leaving matches. More...

Inherits MonoBehaviour.

Public Member Functions

IEnumerator ConnectToMatchmaker ()
 Connect to the matchmaking server. This called automatically in Start(). More...
 
void CreateMatch (int v, Dictionary< string, MatchData > matchData, object onMatchCreated)
 
void CreateMatch (int maxClients, Dictionary< string, MatchData > matchData=null, Action< bool, Match > onCreateMatch=null)
 Send the command to the matchmaking server to create a new match. More...
 
void DestroyMatch (Action< bool > onDestroyMatch=null)
 Destroy a match. This also removes all Client entries and MatchData on the matchmaking server. More...
 
void GetMatch (Action< bool, Match > onGetMatch, int id, bool includeMatchData=true)
 Get info on a single match More...
 
void GetMatchList (Action< bool, Match[]> onMatchList, int pageNumber=0, int resultsPerPage=10, List< MatchFilter > filters=null, bool includeMatchData=true)
 Get the match list, optionally filtering the results. More...
 
void JoinMatch (Match match, Action< bool, Match > onJoinMatch=null)
 Join one of the matches returned my GetMatchList(). More...
 
void LeaveMatch (Action< bool > onLeaveMatch=null)
 Leave a match. More...
 
void SetMatchData (string key, MatchData matchData, Action< bool, Match > onSetMatchData=null)
 Set a single MatchData value and immediately send it to the matchmaking server. More...
 
void SetMatchData (Dictionary< string, MatchData > matchData, Action< bool, Match > onSetMatchData=null)
 Replace all existing match data with new match data. More...
 
void UpdateMatchData (Dictionary< string, MatchData > additionalData, Action< bool, Match > onUpdateMatchData=null)
 Merge new MatchData with existing MatchData and immediately send it all to the matchmaking server. More...
 
void UpdateMatchData (Action< bool, Match > onUpdateMatchData=null)
 Send current MatchData to the matchmaking server. More...
 

Static Public Member Functions

static IEnumerator FetchExternalIP (string ipSource)
 Fetch the external IP More...
 
static string GetLocalAddress (AddressFamily family)
 Gets the a local address by looping through all network interfaces and returning first address from the first interface whose OperationalStatus is Up and whose address family matches the provided family. More...
 
static string PickCorrectAddressToConnectTo (string hostExternalIP, string hostInternalIP)
 Select between internal and external IP. More...
 

Public Attributes

Match currentMatch
 The current Match. This is set whenever a Match is created or joined. More...
 
string externalIPSource = "ipv4.noblewhale.com"
 A web service to query to retrieve the external IP of this computer. More...
 
int matchmakerPort = 20204
 The port to connect to on the matchmaking server. More...
 
string matchmakerURL = "noblewhale.com"
 The url of the matchmaking server. More...
 
int maxReconnectionAttempts = 5
 How many times to attempt to re-connect to the matchmaking server if connection is lost. More...
 
Action onLostConnectionToMatchmakingServer = null
 You can use this action to be informed if connection is lost to the matchmaking server More...
 
float timeout = 5
 How long to wait for a response before giving up More...
 

Static Public Attributes

static string externalIP
 The externalIP which is fetched from an external server More...
 

Protected Member Functions

virtual void Update ()
 Check for incoming responses from the matchmaking server. More...
 

Properties

bool IsReady [get]
 Returns true when the matchmaking server has been succesfully connected to. More...
 

Detailed Description

Provides functionality for creating, listing, joining, and leaving matches.

Opens up a tcp socket connection to the matchmaking server in order to send commands and receive responses. Most methods take an optional callback parameter which you can use to get the response once it is received.

Member Function Documentation

◆ ConnectToMatchmaker()

IEnumerator ConnectToMatchmaker ( )

Connect to the matchmaking server. This called automatically in Start().

Called automatically on start and also when reconnecting after a lost connection. You can also call it yourself if you implement your own re-connection scheme or for whatever other reason you may find.

Returns

◆ CreateMatch()

void CreateMatch ( int  maxClients,
Dictionary< string, MatchData matchData = null,
Action< bool, Match onCreateMatch = null 
)

Send the command to the matchmaking server to create a new match.

Parameters
maxClientsThe maximum number of clients to allow. Once a match is full it is no longer returned in match listings (until a client leaves).
matchDataOptional match data to include with the match. This is a good place to store your connection data.
matchNameThe name of the match.
onCreateMatchOptional callback method to call when a response is received from the matchmaking server.

◆ DestroyMatch()

void DestroyMatch ( Action< bool >  onDestroyMatch = null)

Destroy a match. This also removes all Client entries and MatchData on the matchmaking server.

Parameters
onDestroyMatchOptional callback method to call when a response is received from the matchmaking server.

◆ FetchExternalIP()

static IEnumerator FetchExternalIP ( string  ipSource)
static

Fetch the external IP

Parameters
ipSourceThe url from which to fetch the IP

◆ GetLocalAddress()

static string GetLocalAddress ( AddressFamily  family)
static

Gets the a local address by looping through all network interfaces and returning first address from the first interface whose OperationalStatus is Up and whose address family matches the provided family.

Returns
The local address as a string or an empty string if there is none

◆ GetMatch()

void GetMatch ( Action< bool, Match onGetMatch,
int  id,
bool  includeMatchData = true 
)

Get info on a single match

Parameters
onGetMatchCallback method to call when the response is received from the matchmaking server
idThe ID of the match to fetch info for
includeMatchDataWhether or not to include match data in the response

◆ GetMatchList()

void GetMatchList ( Action< bool, Match[]>  onMatchList,
int  pageNumber = 0,
int  resultsPerPage = 10,
List< MatchFilter filters = null,
bool  includeMatchData = true 
)

Get the match list, optionally filtering the results.

Ex:

var filters = new List<MatchFilter>(){
new MatchFilter("eloScore", 100, MatchFilter.OperationType.GREATER),
new MatchFilter("eloScore", 300, MatchFilter.OperationType.LESS)
};
matchUp.GetMatchList(OnMatchList, filters);
...
void OnMatchList(bool success, Match[] matches)
{
matchUp.JoinMatch(matches[0], OnJoinMatch);
}
Parameters
onMatchListCallback method to call when a response is received from the matchmaking server.
pageNumberUsed with resultsPerPage. Determines which page of results to return. Defaults to 0.
resultsPerPageUser with pageNumber. Determines how many matches to return for each page. Defaults to 10.
filtersOptional List of Filters to use when fetching the match list
includeMatchDataBy default match data is included for every match in the list. If you don't need / want this you can pass false in here and save some bandwidth. If you don't retrieve match data here you can still get it when joining the match.

◆ JoinMatch()

void JoinMatch ( Match  match,
Action< bool, Match onJoinMatch = null 
)

Join one of the matches returned my GetMatchList().

You can use the callback to get the Match object after it is received from the matchmaking server. Once the match is joined you'll have access to all the match's MatchData.

Parameters
matchThe Match to join. Generally this will come from GetMatchList()
onJoinMatchOptional callback method to call when a response is received from the matchmaking server.

◆ LeaveMatch()

void LeaveMatch ( Action< bool >  onLeaveMatch = null)

Leave a match.

Parameters
onLeaveMatchOptional callback method to call when a response is received from the matchmaking server.

◆ PickCorrectAddressToConnectTo()

static string PickCorrectAddressToConnectTo ( string  hostExternalIP,
string  hostInternalIP 
)
static

Select between internal and external IP.

Most of the time we connect to the externalIP but when connecting to another PC on the same local network or another build on the same computer we need to use the local address or localhost instead

Parameters
hostExternalIPThe host's external IP
hostInternalIPThe host's internal IP
Returns

◆ SetMatchData() [1/2]

void SetMatchData ( string  key,
MatchData  matchData,
Action< bool, Match onSetMatchData = null 
)

Set a single MatchData value and immediately send it to the matchmaking server.

Ex:

matchUp.SetMatchData("eloScore", 100);
Parameters
keyThe key
matchDataThe value
onSetMatchDataOptional callback method to call when a response is received from the matchmaking server.

◆ SetMatchData() [2/2]

void SetMatchData ( Dictionary< string, MatchData matchData,
Action< bool, Match onSetMatchData = null 
)

Replace all existing match data with new match data.

Ex:

var newMatchData = new Dictionary<string, MatchData>() {
{ "Key1", "value1" },
{ "Key2", 3.14159 }
};
matchUp.SetMatchData(newMatchData);
Parameters
matchDataA Dictionary of new MatchData
onSetMatchDataOptional callback method to call when a response is received from the matchmaking server.

◆ Update()

virtual void Update ( )
protectedvirtual

Check for incoming responses from the matchmaking server.

◆ UpdateMatchData() [1/2]

void UpdateMatchData ( Dictionary< string, MatchData additionalData,
Action< bool, Match onUpdateMatchData = null 
)

Merge new MatchData with existing MatchData and immediately send it all to the matchmaking server.

Ex:

var additionalMatchData = new Dictionary<string, MatchData>() {
{ "Key1", new MatchData("value1") },
{ "Key2", new MatchData(3.14159) }
};
matchUp.UpdateMatchData(additionalMatchData);
Parameters
additionalDataA Dictionary of additional MatchData to merge into existing match data
onUpdateMatchDataOptional callback method to call when a response is received from the matchmaking server.

◆ UpdateMatchData() [2/2]

void UpdateMatchData ( Action< bool, Match onUpdateMatchData = null)

Send current MatchData to the matchmaking server.

Ex:

matchUp.currentMatch.matchData["Key1"] = 3.14159;
matchUp.currentMatch.matchData["Key2"] = "Hello world";
matchUp.UpdateMatchData();
Parameters
onUpdateMatchDataOptional callback method to call when a response is received from the matchmaking server.

Member Data Documentation

◆ currentMatch

Match currentMatch

The current Match. This is set whenever a Match is created or joined.

◆ externalIP

string externalIP
static

The externalIP which is fetched from an external server

◆ externalIPSource

string externalIPSource = "ipv4.noblewhale.com"

A web service to query to retrieve the external IP of this computer.

◆ matchmakerPort

int matchmakerPort = 20204

The port to connect to on the matchmaking server.

Leave this as default unless you started the server on a specific port using

./MatchUp -p [PORT NUMBER]

◆ matchmakerURL

string matchmakerURL = "noblewhale.com"

The url of the matchmaking server.

You can use mine for testing but it could go offline at any time so don't even think of trying to release a game without hosting your own matchmaking server.

◆ maxReconnectionAttempts

int maxReconnectionAttempts = 5

How many times to attempt to re-connect to the matchmaking server if connection is lost.

When the connection is lost the Matchmaker will automatically try and reconnect this many times. If you wish to implement your own reconnect behaviour, use the onLostConnectonToMatchmakingServer action and set this to 0 to disable the default behaviour.

◆ onLostConnectionToMatchmakingServer

Action onLostConnectionToMatchmakingServer = null

You can use this action to be informed if connection is lost to the matchmaking server

Generally this means someone pulled out the internet plug or something equally catastrophic. If the connection is lost on the host, the current match is destroyed. If the connection is lost on the client, the current match is left. When the connection is lost the Matchmaker will automatically try and reconnect. If you wish to implement your own reconnect behaviour make sure to set maxReconnectionAttempts to 0 to disable the default behaviour.

◆ timeout

float timeout = 5

How long to wait for a response before giving up

Property Documentation

◆ IsReady

bool IsReady
get

Returns true when the matchmaking server has been succesfully connected to.