Class NetSessionManager

Manages a networked session with multiple peer connections.

Usage: - Call StartHost() to begin accepting connections. - Call JoinSession() to connect to an existing host. - Call Update() every frame to process network events. - Subscribe to events to handle connections and disconnections.

NetSessionManager is the main entry point for networking. It handles hosting/joining sessions, managing connected peers, and routing messages between them.

Hierarchy

  • FrameworkObject
    • NetSessionManager

Constructors

  • Parameters

    • OptionalisInternalConstructor: boolean

    Returns NetSessionManager

Accessors

  • get hostPort(): number
  • Returns the port the host is listening on (0 if not hosting).

    Returns number

    Host only.

  • get isAuthority(): boolean
  • Returns true if this node is the authority (host or dedicated).

    Returns boolean

  • get isSessionActive(): boolean
  • Returns true if the session is active (hosting or connected).

    Returns boolean

  • get localConnectionId(): number
  • Returns the local connection ID.

    Returns number

  • get peerCount(): number
  • Returns the number of connected peers.

    Returns number

    The peer count (0 if not hosting).

    Host only.

  • get serverTimeEpochMs(): number
  • Returns the current server time estimate (epoch ms).

    Returns number

  • get sessionId(): string
  • Returns the session ID.

    Returns string

Methods

  • Disconnects a specific peer (host only).

    Parameters

    • connectionId: number

      Connection ID of the peer to disconnect.

    • reason: string

      Reason for disconnection.

    Returns void

  • Returns information about a specific peer.

    Parameters

    • connectionId: number

      Connection ID of the peer.

    Returns NetPeerInfo

    Peer info if found, or empty if not found.

  • Initiates a scene transition for all connected peers (host only).

    Parameters

    • scene: Scene | ResourceHandle<Scene>

      The scene resource to load and instance.

    • Optionalseamless: boolean

      If true, preserve non-scene objects during transition.

    Returns void

    Only valid when hosting. The scene will be instanced locally and a load request will be broadcast to all clients.

  • Joins an existing session as a client.

    Parameters

    Returns boolean

    True if connection attempt started.

  • Notifies the server that scene loading is complete. Client only.

    Parameters

    • loadedScene: SceneInstance

      The scene instance that was loaded.

    • Optionalsuccess: boolean

      True if scene loaded successfully.

    • OptionalerrorMessage: string

      Error message if loading failed.

    Returns void

    Call this after loading and instancing the scene requested by OnClientSceneLoadRequested. The scene instance will be used to set up the client's session context.

  • Sends a Multicast RPC to specified clients (host only).

    Parameters

    • rpcName: string

      The name of the RPC to invoke.

    • rpcObjectId: number

      The network object ID of the target instance.

    • rpcArgs: DataStream

      Serialized argument payload.

    • connectionIds: number[]

      The target client connection IDs.

    Returns UncertainAsyncOp<RpcCallResult[], string>

    async operation

    This is a convenience method for hosts to broadcast RPCs to multiple clients. The replication manager handles routing to the specified connections.

  • Sends a Client RPC to a specific connection (host only).

    Parameters

    • rpcName: string

      The name of the RPC to invoke.

    • rpcObjectId: number

      The network object ID of the target instance.

    • rpcArgs: DataStream

      Serialized argument payload.

    • connectionId: number

      The target client connection ID.

    Returns UncertainAsyncOp<RpcCallResult, string>

    async operation

    This is a convenience method for hosts to send RPCs to specific clients. The call's Targets will be set to the specified connection.

  • Sends a Server RPC to the host (client only).

    Parameters

    • rpcName: string

      The name of the RPC to invoke.

    • rpcObjectId: number

      The network object ID of the target instance.

    • rpcArgs: DataStream

      Serialized argument payload.

    Returns UncertainAsyncOp<RpcCallResult, string>

    async operation

  • Sends a Client RPC to the owner of a network object (host only).

    Parameters

    • rpcName: string

      The name of the RPC to invoke.

    • objectId: number

      The network object ID to find the owner for.

    • rpcArgs: DataStream

      Serialized argument payload.

    Returns UncertainAsyncOp<RpcCallResult, string>

    async operation

    Looks up the network object by its ID, retrieves the owner connection, and sends the RPC to that connection. If the object has no owner, the call is aborted.

  • Starts hosting a session on the default port.

    Parameters

    • projectId: string

      Project identifier.

    • sessionId: string

      Session identifier.

    • buildHash: string

      Build hash for version validation.

    Returns boolean

    True if hosting started successfully.

  • Starts hosting a session on a specific port.

    Parameters

    • projectId: string

      Project identifier.

    • sessionId: string

      Session identifier.

    • buildHash: string

      Build hash for version validation.

    • port: number

      TCP port to listen on.

    Returns boolean

    True if hosting started successfully.

  • Stops the current session (hosting or connected).

    Returns void

  • Updates the session, processing network events.

    Parameters

    • deltaTime: number

      Time since last update in seconds.

    Returns void

    Must be called regularly (typically once per frame).

Events

onHostPeerConnected: Event<[peerInfo: NetPeerInfo], void>

Event triggered when a new peer connects. Host only.

This event should be used to setup the initial PlayerState for the connection.

Information about the connected peer.

onHostPeerDisconnected: Event<[connectionId: number, reason: string], void>

Event triggered when a peer disconnects. Host only.

Connection ID of the disconnected peer.

Disconnect reason.

onSessionStarted: Event<[], void>

Event triggered when the session starts successfully. Both host and client.

For the host, this event is triggered once the session is ready to accept connections. For the client, this event is triggered once the client has successfully joined the session.

onSessionStopped: Event<[reason: string], void>

Event triggered when the session is stopped or disconnected. Both host and client.

For the host, this event is triggered when hosting stops and all clients are disconnected. For the client, this event is triggered when the client is disconnected from the host.

Reason for the session ending.

onClientJoinFailed: Event<[reason: string], void>

Event triggered when joining a session fails. Client only.

This event is triggered if the client fails to join the session, a reason is provided but the session is not connected.

Failure reason.

onClientSceneLoadRequested: Event<[sceneUUID: UUID, sceneName: string, seamless: boolean, transitionId: number], void>

Event triggered when the server requests a scene load. Client only.

UUID of the scene to load.

Name of the scene for logging/display.

True if this is a seamless transition.

Unique ID for this transition.

onClientSceneActivate: Event<[transitionId: number], void>

Event triggered when the scene should activate. Client only.

This event is triggered once the client has loaded the scene and the server switches to active game play.

The transition that completed.

onClientGameStateReplicated: Event<[gameState: Resource], void>

Event triggered when the GameState was replicated. Client only.

The replicated game state.

onClientPlayerStateReplicated: Event<[playerState: Resource], void>

Event triggered when the PlayerState of the current client was replicated. Client only.

The replicated player state.

onClientObjectReplicated: Event<[netObjectId: number], void>

Event triggered when an object was replicated. Client only.

The NetObjectId of the replicated object.

onHostSceneTransitionComplete: Event<[transitionId: number], void>

Event triggered when all clients have loaded the scene. Host only.

The transition that completed.

onHostPeerSceneLoadComplete: Event<[connectionId: number], void>

Event triggered when a peer completes scene loading.

This event should be used to track when clients are ready. The server will send a scene activate message to the client in an active scene, or wait until all clients are ready to activate the scene for everyone. If the scene is already active, the server will immediately send the activate message to the client upon receiving this event.

ID of the connection that completed loading.

onHostPeerActivatedInScene: Event<[connectionId: number], void>

Event triggered when a peer is activated in the scene upon completion of loading.

The event is after the host completes the scene transition internally, but before the OnSceneTransitionComplete event is sent.

If the scene is already active, the event is triggered immediately after the client completes loading and is activated.

It's important to distinguish this from OnPeerSceneLoadComplete, which is triggered as soon as the client finishes loading the scene,

This event can also be triggered on a stale client that did not finish loading the scene in time, but the server decided to transition to active state.

This event indicates that the client has not only loaded the scene but has also been activated within it. This is the point where the client is fully ready to participate in the session.

ID of the connection that completed loading.