How To: Create a Network Session

This article discusses the details of creating a new network session.

    To Describe Session Properties

    A network session is made up of the players in the game and an optional set of attributes to aid in describing the type of session that is being created. The attributes that are used to describe an available multiplayer session are known as session properties. These session properties are created by you to describe this particular type of multiplayer session for your particular game, and might contain descriptions of details such as a whether this game is a timed session or capture-the-flag session.

    Creating a NetworkSessionProperties collection to describe your session is optional. You can also pass in null when creating a new network session.
    1. The NetworkSessionProperties supports up to eight integer values that describe the session. Because these values are integers, in this example we will create a set of enums to describe these integer values and their placement in the NetworkSessionProperties.

      enum SessionProperty{ GameMode, SkillLevel, ScoreToWin }
      enum GameMode{ Practice, Timed, CaptureTheFlag }
      enum SkillLevel{ Beginner, Intermediate, Advanced }
    2. Create a new NetworkSessionProperties, and set the values to the custom session properties.

      NetworkSessionProperties sessionProperties = new NetworkSessionProperties();
          = (int)GameMode.Practice; // Game mode
          = (int)SkillLevel.Beginner; // Score to win
          = 100;

    To Create the Session

    There are multiple types of sessions that can be created, such as a local session (used for split-screen gaming and requiring no network traffic), system link sessions (which connect multiple gaming machines over a local subnet), or Xbox LIVE matchmaking sessions, which take place over the Internet. In this example we will create a system link game.

    The gaming machine which creates the session is called the host, and owns the multiplayer session. The host does not imply a game server or authority. There are no network topologies implied, such as client-server, peer-to-peer, or hybrid. The network topology will be determined by your implementation of your game.
    NetworkSession session;
        int maximumGamers = 8;  // The maximum supported is 31
        int privateGamerSlots = 2;
        int maximumLocalPlayers = 1;
        // Create the session
        session = NetworkSession.Create( 
            maximumLocalPlayers, maximumGamers, privateGamerSlots, 
            sessionProperties );

    To enable host migration and join-in-progress

    Set the AllowJoinInProgress and AllowHostMigration properties to values appropriate for you game.

    session.AllowHostMigration = true;
    session.AllowJoinInProgress = true;

    To Subscribe to Session Events

    Subscribe to any session events that your game is interested in. Multiplayer sessions have events that occur when the game transitions from lobby to gameplay (GameStarted and GameEnded), when players join and leave (GamerJoined and GamerLeft), when the host changes (HostChanged), and when the session ends (SessionEnded).

    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);

    To End the Session

    If you are a host ending the session purposefully and the conditions for ending the session have been met, call NetworkSession.EndGame. In this example, when the host presses the ESC key or Back button indicating that the player wishes to exit the game, the host checks if there are players remaining in the game before ending the session.

    // Check for exit.
    if (gamePad.Buttons.Back == ButtonState.Pressed)
        if (session.AllGamers.Count == 1)
        session = null;

    The end of a game is controlled by the NetworkSession.EndGame method. At the end of a game, you might choose to move back to the lobby or to end the session. Players in the game can subscribe to the NetworkSession.GameEnded and NetworkSession.SessionEnded events.

    Community Additions