Hosted Matches
Hosted matches allow your game to use Game Center’s matchmaking service to find players, but use your own server to connect the players to each other. Hosted matches require you to implement your own custom low-level networking code, because Game Center’s servers are not involved in the match play all. However, hosted games offer larger match sizes and the ability to add your own server into every match.
Matching Players for a Hosted Match
The code you write for hosted matchmaking is similar to the code you write for real-time matches. Before you attempt to create a game that uses hosted matches, you should already be familiar with real-time matchmaking. The critical difference between the two models is that hosted matchmaking returns player identifiers instead of a completed match object. Your game then connects that player to your own server. Your server must that player’s network address to the player identifier and, similar to a match, perform routing when one device in the match needs to communicate with others.
Table 9-1 lists the most common methods from real-time matchmaking and the corresponding method or implementation details for hosted matchmaking.
Real-time matchmaking | Hosted matchmaking |
|---|---|
Your view controller delegate implements the | Your delegate implements the |
Your programmatic matchmaking code calls | Your programmatic code calls |
Your programmatic code calls | Your programmatic code calls |
Your game receives an invitation. | The |
Hosted Matches Overview
By default, when you perform the matchmaking process, Game Kit returns an instance of the GKMatch object on each device connected to the match. Each device then uses its match object to communicate with other participants. For most games, this default behavior is exactly what you want. The GKMatch class abstracts the underlying network topology so that your game can simply focus on sending data to other participants without worrying about how the data is transmitted.
However, some games may want to support more players than the maximum number of supported players on Game Center, or may want their own server to arbitrate the match. In these cases, you can still use matchmaking to find players for a hosted match. A hosted match does not use GKMatch objects. Instead, each copy of your game receives the player identifiers for all of the players in the match. Your game then takes additional steps to connect the players to your server.
Creating a hosted match requires your game to implement all of the low-level networking required for your game. In particular, you must do all of the following in your game:
Design and implement your own networking code to connect each device to your server.
Design and implement your own networking protocol to inform other devices of the state of any participant in the match.
Design and implement your own server implementation to map player identifiers to the specific device connected to your server. Thus, your server becomes responsible for routing network data between players.
If your game uses Game Kit’s standard matchmaking user interface, you must make sure each device informs Game Kit after it connects to your server. This information allows Game Kit to update its user interface.
Creating a Hosted Match Using the Matchmaker View Controller
To create a hosted match through the matchmaker view controller, you set the view controller’s hosted property to YES before presenting the view controller. Listing 9-1 shows a simple example of this.
Listing 9-1 Creating a hosted match
- (IBAction)hostMatch: (id) sender |
{ |
GKMatchRequest *request = [[GKMatchRequest alloc] init]; |
request.minPlayers = 2; |
request.maxPlayers = 2; |
GKMatchmakerViewController *mmvc = [[GKMatchmakerViewController alloc] initWithMatchRequest:request]; |
mmvc.matchmakerDelegate = self; |
mmvc.hosted = YES; |
[self presentViewController:mmvc animated:YES completion:nil]; |
} |
When the view controller is hosted, your delegate implements the matchmakerViewController:didReceiveAcceptFromHostedPlayer: method to process the new player. Your delegate should behave differently depending on which device your game is running on:
When called on the player’s device, the device should connect to your own server. When successfully connected, it should call the
setHostedPlayer:connected:method to tell the view controller that the player has connected to the match.When called on another player’s device, your game should determine that it can talk to the new player’s device through your server. Once it knows it can send messages to the other client, it should call the
setHostedPlayer:connected:method to update the user interface.
© 2012 Apple Inc. All Rights Reserved. (Last updated: 2012-09-19)