次の方法で共有


How To: Create a Network Session

  • Conducting a Network Session

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. They might contain descriptions of details such as a whether this game is a timed session or capture-the-flag session.

Note

Creating a NetworkSessionProperties collection to describe your session is optional. You can also pass in null when creating a new network session.

The NetworkSessionProperties supports up to eight integer values that describe the session. Because these values are integers, we will create a set of enums in this example 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 }

Conducting a Network Session

To describe session properties

  1. Create a new NetworkSessionProperties.

  2. Set the values to the custom session properties.

    NetworkSessionProperties sessionProperties = new NetworkSessionProperties();
    
    sessionProperties[(int)SessionProperty.GameMode] 
        = (int)GameMode.Practice; 
    sessionProperties[(int)SessionProperty.SkillLevel] 
        = (int)SkillLevel.Beginner; 
    sessionProperties[(int)SessionProperty.ScoreToWin]
        = 100;
    

You can create many different types of sessions. One is a local session, which is used for split-screen gaming and requires no network traffic. Another is a system link session, which connects multiple gaming machines over a local subnet. Finally, there is an Xbox LIVE multiplayer session, which takes place over the Internet. In this example, we will create a system link game.

Note

The gaming machine that creates the session is called the host. It 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;  
    int privateGamerSlots = 2;
    int maximumLocalPlayers = 1;

    // Create the session
    session = NetworkSession.Create( 
        NetworkSessionType.SystemLink,
        maximumLocalPlayers, maximumGamers, privateGamerSlots, 
        sessionProperties );

To enable host migration and join-in-progress functionality

To subscribe to session events

  • Subscribe to any session events in which your game has an interest.

    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 wants to exit the game, the host checks to see whether there are players still in the game. If not, the host ends the session.

    // Check for exit.
    if ( IsButtonPressed(GamePadButton.B ) || IsButtonPressed( GamePadButton.Back ) )
    {
        if (session != null)
        {
            if (session.AllGamers.Count == 1)
            {
                session.EndGame();
                session.Update();
            }
        }
    }
    

    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.

See Also

Concepts

Network Session Management
Third-Party Firewall Settings

Tasks

How To: Find and Join a Network Session