NetworkSession

Abstract base class that manages multiplayer session state. Tracks connected players, handles connection approval, and synchronizes join/leave events across all clients using the Paragon messaging system. Platform-specific network services subclass this to implement session creation, leaving, and player kicking.

Definition

Namespace: Paragon.Core.Network Assembly: Paragon.dll

public abstract class NetworkSession

Remarks

NetworkSession is the core abstraction for multiplayer sessions. It operates on an owner-authoritative model:

  • The session owner (host) handles connection approval, maintains the authoritative player list, and broadcasts join/leave events to all other clients.

  • Non-owner clients receive the initial player list via SynchronizationMessage and subsequent updates via PlayerJoinedMessage / PlayerLeftMessage.

The constructor automatically subscribes to Netcode client callbacks and Paragon network messages. The session considers itself synchronized immediately if the current player is the owner; otherwise, it waits for a SynchronizationMessage from the owner.

Connection approval is delegated to OnConnectionApproval, which rejects duplicate users (same service ID already in session) and queues approved connections as PendingConnection records until the Netcode OnClientConnected callback fires.

Quick Lookup

Goal
How

Get session ID

session.Id

Get session name

session.Name

Check if local player owns session

session.IsOwner

Get all connected players

session.Players

Get the session owner

session.Owner

Find a player by client ID

session.GetPlayer(clientId)

Find a player by user ID

session.GetPlayer(userId)

Check if a player is connected

session.HasPlayer(clientId) or session.HasPlayer(userId)

Leave the session

await session.Leave()

Kick a player (owner only)

await session.KickPlayer(player)

Listen for player joins

session.PlayerJoined += OnPlayerJoined

Listen for player departures

session.PlayerLeft += OnPlayerLeft

Tear down the session

session.Shutdown()

Properties

Id

The unique identifier for this session.

Name

The display name of this session.

MaxConnections

The maximum number of players allowed in this session.

Data

Custom key-value metadata associated with the session.

Players

The set of all currently connected players.

Owner

The NetworkPlayer who owns (hosts) this session.

CurrentPlayer

The NetworkPlayer representing the local client.

IsOwner

Whether the local client is the session owner.

Events

PlayerJoined

Raised when a new player joins the session (on all clients).

PlayerLeft

Raised when a player leaves the session (on all clients).

Methods

Constructor

Parameter
Type
Description

id

string

Unique session identifier

name

string

Display name of the session

maxConnections

int

Maximum allowed players

ownerID

string

User ID of the session owner

data

Dictionary<string, string>

Custom session metadata

Initializes session state, creates the local NetworkPlayer, subscribes to Netcode callbacks (OnClientConnectedCallback, OnClientDisconnectCallback) and Paragon messages (SynchronizationMessage, PlayerJoinedMessage, PlayerLeftMessage).

Leave (abstract)

Leaves the current session. Implementation is platform-specific.

KickPlayer (abstract)

Kicks a player from the session. Implementation is platform-specific.

Parameter
Type
Description

player

NetworkPlayer

The player to remove from the session

HasPlayer

Checks whether a player with the given identifier is in the session.

Parameter
Type
Description

clientID

ulong

The Netcode client ID to search for

userID

string

The service user ID to search for

Returns: true if a matching player is in the session.

GetPlayer

Retrieves a player by identifier. Throws if not found.

Parameter
Type
Description

clientID

ulong

The Netcode client ID to search for

userID

string

The service user ID to search for

Returns: The matching NetworkPlayer.

OnConnectionApproval

Called by the Netcode connection approval handler. Validates incoming connections and queues approved clients as pending.

Parameter
Type
Description

request

ConnectionApprovalRequest

The incoming connection request with payload

response

ConnectionApprovalResponse

The response to approve or reject

Shutdown

Clears all session state and unsubscribes from Netcode callbacks. Call this when the session ends.

ToString

Returns a string representation of the session.

Returns: "id - (name)"

Extension Points

Required Overrides

Method
Purpose

Leave()

Platform-specific session departure logic

KickPlayer(NetworkPlayer)

Platform-specific player removal logic

Optional Overrides

Method
Purpose

OnSynchronizationMessage(SynchronizationMessage)

Customize how the player list is rebuilt on sync. Call base to preserve default behavior.

OnPlayerJoined(NetworkPlayer)

React to a player joining. Call base to add to Players and broadcast the event.

OnPlayerLeft(NetworkPlayer)

React to a player leaving. Call base to remove from Players and broadcast the event.

Implementation Requirements

When subclassing NetworkSession, you MUST:

  1. Implement Leave() to handle platform-specific session departure

  2. Implement KickPlayer() to handle platform-specific player removal

  3. Call base.OnPlayerJoined() / base.OnPlayerLeft() if overriding those methods (to maintain player tracking and event broadcasting)

  4. Call base.OnSynchronizationMessage() if overriding (to rebuild the player list)

You SHOULD:

  • Call Shutdown() when the session is no longer needed to avoid callback leaks

  • Subscribe to PlayerJoined / PlayerLeft events for game-level reactions

Common Pitfalls

circle-exclamation
circle-exclamation
circle-exclamation

Examples

Subscribing to Session Events

Querying Players

Implementing a Custom Session

See Also

Last updated