Export (0) Print
Expand All

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.

Bb975500.note(en-US,XNAGameStudio.30).gifNote
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

  1. Create a NetworkSessionProperties collection.

  2. 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

  1. Create an AvailableNetworkSessionCollection to hold the search results.

    AvailableNetworkSessionCollection availableSessions;
    
  2. Use NetworkSession.Find to retrieve the list of available sessions.

  3. 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);
    
    Bb975500.note(en-US,XNAGameStudio.30).gifNote
    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

  1. Create a NetworkSession.

    NetworkSession session;
    
  2. Join the AvailableNetworkSession using NetworkSession.Join.

    session = NetworkSession.Join(availableSessions[selectedSessionIndex]);
    
  3. 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);
    

Community Additions

ADD
Show:
© 2014 Microsoft