How To: Find and Join a Network Session
Describes how to find and join a network session.
The NetworkSessionProperties collection used to create a session with specific properties can also be used to search for sessions with particular properties. Because these values are integers, we will create a set of enumerations in this example to describe these integer values and their placement in the NetworkSessionProperties collection.
Note
Setting session properties to use as search parameters is an optional step. You may also pass in an empty NetworkSessionProperties to NetworkSession.Find or NetworkSession.BeginFind to view all available sessions regardless of property settings.
enum SessionProperty{ GameMode, SkillLevel, ScoreToWin }
enum GameMode{ Practice, Timed, CaptureTheFlag }
enum SkillLevel{ Beginner, Intermediate, Advanced }
Joining a Network Session
To set the search parameters
Create a NetworkSessionProperties collection.
Set the values to the desired search parameters.
NetworkSessionProperties searchProperties = new NetworkSessionProperties(); searchProperties[(int)SessionProperty.GameMode] = (int)GameMode.Practice; searchProperties[(int)SessionProperty.SkillLevel] = (int)SkillLevel.Beginner;
To find an available network session
Create an AvailableNetworkSessionCollection to hold the search results.
AvailableNetworkSessionCollection availableSessions;
Use NetworkSession.Find to retrieve the list of available sessions.
Specify the type of session in which you are interested, the number of local players that wish to join, and any session properties that should be matched when searching.
Note that the searchProperties argument is optional. You may pass in null to match any available session regardless of the session properties settings for that session.
int maximumLocalPlayers = 1; availableSessions = NetworkSession.Find( NetworkSessionType.SystemLink, maximumLocalPlayers, searchProperties);
Note
This search may also be performed asynchronously using NetworkSession.BeginFind and NetworkSession.EndFind.
Once you have a list of available sessions, you can examine the session to determine if you would like to join a particular session. In this example, we will print some information about the available session for the player to examine.
int sessionIndex = 0;
AvailableNetworkSession availableSession = availableSessions[sessionIndex];
string HostGamerTag = availableSession.HostGamertag;
int GamersInSession = availableSession.CurrentGamerCount;
int OpenPrivateGamerSlots = availableSession.OpenPrivateGamerSlots;
int OpenPublicGamerSlots = availableSession.OpenPublicGamerSlots;
string sessionInformation = "Session available from gamertag " + HostGamerTag +
"\n" + GamersInSession + " players already in this session. \n" +
+OpenPrivateGamerSlots + " open private player slots available. \n" +
+OpenPublicGamerSlots + " public player slots available.";
spriteBatch.DrawString(spriteFont, sessionInformation, new Vector2(100, y), color);
To join an available multiplayer session
Create a NetworkSession.
NetworkSession session;
Join the AvailableNetworkSession using NetworkSession.Join.
session = NetworkSession.Join(availableSessions[selectedSessionIndex]);
After joining a NetworkSession, subscribe to any session events that the peer might be interested in.
session.GamerJoined += new EventHandler<GamerJoinedEventArgs>(session_GamerJoined); session.GamerLeft += new EventHandler<GamerLeftEventArgs>(session_GamerLeft); session.GameStarted += new EventHandler<GameStartedEventArgs>(session_GameStarted); session.GameEnded += new EventHandler<GameEndedEventArgs>(session_GameEnded); session.SessionEnded += new EventHandler<NetworkSessionEndedEventArgs>(session_SessionEnded);