Skip to content

Latest commit

 

History

History
33 lines (24 loc) · 3 KB

part002.md

File metadata and controls

33 lines (24 loc) · 3 KB

How it works, part 2: connection and ready flow

LiteNetLibGameManager extends LiteNetLibManager with requests, scene loading, player readiness, objects, RPCs, and state synchronization. The game request IDs (GameReqTypes) are carried inside the request/response message types (GameMsgTypes.Request and Response); they are not separate top-level packet IDs.

Join sequence

  1. The server accepts a transport connection, assigns a connection ID, and creates a LiteNetLibPlayer.
  2. The client's OnClientConnected() sends SendClientEnterGame() unless doNotEnterGameOnConnect is true. The request contains the packet version and any bytes written by SerializeEnterGameData.
  3. The server's DeserializeEnterGameData checks the request. The default implementation compares packet versions. On success, the enter-game response contains the connection ID and current ServerSceneInfo.
  4. The client stores ClientConnectionId and loads or initializes the server scene. On later server scene changes, GameMsgTypes.ServerSceneChange carries the new scene information.
  5. After the online scene is ready, the client sends SendClientReady() unless doNotReadyOnSceneLoaded is true. The server calls SetPlayerReady, which marks the player ready, spawns the player prefab when configured, and invokes DeserializeClientReadyData.
  6. The interest manager subscribes the ready player to eligible objects. Spawn state is queued for delivery to that client.
Client                         Server
  |---- transport connect ------>|
  |---- EnterGame request ------->| validate version/custom data
  |<--- EnterGame response -------| connection ID + scene info
  | load/initialize online scene  |
  |---- ClientReady request ----->| mark ready, spawn player
  |<--- subscribed spawn state ---| object IDs + initial state

The built-in top-level message IDs include Request = 0, Response = 1, RPC = 2, SyncBaseLine = 3, SyncDelta = 4, and ServerSceneChange = 6. GameReqTypes.EnterGame = 0 and ClientReady = 1 identify requests within the request message. See part 1 when registering custom packet IDs.

Custom join data

Override the matching writer and reader on your game manager:

  • SerializeEnterGameData(NetDataWriter writer) on the client and DeserializeEnterGameData(uint requestId, long connectionId, EnterGameRequestMessage request, NetDataReader reader) on the server. The reader returns UniTask<bool>; if overriding it, preserve version validation as needed.
  • SerializeClientReadyData(NetDataWriter writer) on the client and DeserializeClientReadyData(uint requestId, long connectionId, NetDataReader reader, LiteNetLibIdentity playerIdentity) on the server. The reader returns UniTask<bool>; playerIdentity comes from SpawnPlayer and may be null when no player prefab is configured.

Write and read the custom fields in the same order. Keep the automatic enter-game and ready flags aligned with your UI flow. For objects and subscriptions after ready, continue to part 3.