Client - jimdroberts/FishMMO GitHub Wiki

Description

Client controls connecting to servers, managing network state, scene loading/unloading, and client-side systems for FishMMO. It handles authentication, reconnection, event management, and UI integration, serving as the main entry point for client logic.


API Access

Fields

  • private Dictionary<int, Scene> loadedWorldScenes

    Dictionary of loaded world scenes, keyed by scene handle. Used to track and unload scenes when changing worlds.

  • private LocalConnectionState clientState

    Current local connection state of the client.

  • private ServerConnectionType currentConnectionType

    Current type of server connection (login, world, scene, etc).

  • private byte reconnectsAttempted

    Number of reconnect attempts made.

  • private float nextReconnect

    Time remaining until next reconnect attempt.

  • private bool forceDisconnect

    If true, forces the client to disconnect from the server.

  • private string lastWorldAddress

    Last world server address used for reconnect attempts.

  • private ushort lastWorldPort

    Last world server port used for reconnect attempts.

  • public List LoginServerAddresses

    List of login server addresses available to the client.

  • public List WorldPreloadScenes

    List of scenes to preload when entering the world.

  • public byte MaxReconnectAttempts

    Maximum number of reconnect attempts allowed.

  • public float ReconnectAttemptWaitTime

    Time to wait between reconnect attempts (in seconds).

  • public ClientPostbootSystem ClientPostbootSystem

    Reference to the client postboot system for scene management.

  • public NetworkManager NetworkManager

    Reference to the network manager for client networking.

  • public ClientLoginAuthenticator LoginAuthenticator

    Reference to the login authenticator for client authentication.

  • public AudioListener AudioListener

    Reference to the audio listener for the client.

Properties

  • public bool CanReconnect

    Returns true if the client can attempt to reconnect (only in world or scene connection states).

Methods

  • void Awake()

    Initializes the client, network manager, authenticator, and other systems.

  • void OnDestroy()

    Cleans up client resources and unregisters event handlers on destroy.

  • public void Quit()

    Quits the application or play mode, depending on platform.

  • public void QuitToLogin(bool forceDisconnect = true)

    Quits to the login screen, disconnects from server, and unloads world scenes.

  • public bool IsConnectionReady([LocalConnectionState clientState = LocalConnectionState.Started], [bool requireAuthentication = false])

    Checks if the current connection is valid and started. Optional authentication check.

  • public void ConnectToServer(string address, ushort port, bool isWorldServer = false)

    Connects to a server at the specified address and port. Optionally marks as world server.

  • public void OnTryReconnect()

    Attempts to reconnect to the last known world server address and port.

  • public void ReconnectCancel()

    Cancels reconnect attempts and quits to login screen.

  • public void ForceDisconnect()

    Forces the client to disconnect from the server.

  • public static void Broadcast(T broadcast, Channel channel = Channel.Reliable) where T : struct, IBroadcast

    Broadcasts a message to the server using the network manager.

  • public bool TryGetRandomLoginServerAddress(out ServerAddress serverAddress)

    Attempts to get a random login server address from the available list.

  • public IEnumerator GetLoginServerList(Action onFetchFail, Action<List> onFetchComplete)

    Coroutine to fetch the login server list from a remote host or configuration.


Basic Usage

Setup

  1. Attach the Client component to a GameObject in your main scene.
  2. Assign required references in the Inspector (e.g., NetworkManager, LoginAuthenticator).
  3. Configure server addresses and preload scenes as needed.
  4. Call ConnectToServer to initiate a connection, or use GetLoginServerList to fetch available servers.

Example

// Example 1: Connect to a World Server
client.ConnectToServer("127.0.0.1", 7777, true);

// Example 2: Fetch Login Server List
StartCoroutine(client.GetLoginServerList(OnFail, OnComplete));

void OnFail(string error) {
    Debug.LogError(error);
}
void OnComplete(List<ServerAddress> addresses) {
    // Use addresses
}

Best Practices

  • Always check IsConnectionReady() before sending network messages.
  • Use event hooks (e.g., OnConnectionSuccessful, OnReconnectAttempt) to update UI or game state.
  • Clean up resources and unregister event handlers in OnDestroy to prevent memory leaks.
  • Configure reconnect attempts and wait times to balance user experience and server load.
⚠️ **GitHub.com Fallback** ⚠️