EzyFox Server Documentation

repository·master·Indexed 20 days ago

https://github.com/youngmonkeys/ezyfox-server

An open-source real-time server engine for multiplayer games and real-time applications. It supports TCP, UDP, WebSocket, and HTTP protocols, featuring a hierarchical architecture of Servers, Zones, Apps, and Plugins. The engine provides a wide range of client SDKs for Mobile (Android, iOS, Flutter, React Native), Web/Desktop (Javascript, React, C++, C# Unity), and Java/Netty.

Tokens
8.6K
Snippets
27
Records
43
Agent score
70%

What's inside EzyFox Server

  1. How EzyFox Server architecture works

    master

    EzyFox Server uses a hierarchical structure to manage users, applications, and logic. Understanding this hierarchy is essential for organizing your game or real-time application:

    • Server: The top-level container that holds multiple Zones.
    • Zone: A logical grouping within the server. Each zone contains its own User Manager, multiple Apps, and multiple Plugins.
    • App: A specific application logic unit within a zone. Each app maintains its own User Manager.
    • Plugin: A specialized component within a zone that focuses exclusively on handling events and clients' requests.

    This separation allows you to isolate different game modes or application features into distinct zones and apps, each with its own user scope.

  2. Available Client SDKs for EzyFox

    master

    EzyFox provides a wide range of client-side SDKs to connect your frontend or mobile applications to the server. Supported platforms include:

    • Mobile: Android, iOS (Swift), Flutter, React Native
    • Web/Desktop: Javascript (ES6 and standard), React, C++, C# (Unity/CSharp)
    • Other: Java, Netty
  3. Use EzyAppObjectResponse to send objects from an application

    master

    When developing application-level commands in EzyFox Server, use EzyAppObjectResponse to wrap and send objects back to clients. This class is a specialized response type that utilizes an EzyMarshaller to serialize objects and leverages the EzyAppContext to create the underlying response via EzyAppResponse. It is designed to bridge the gap between application logic and the server's command response system.

    // Example instantiation within an application context
    EzyAppObjectResponse response = new EzyAppObjectResponse(context, marshaller);
  4. Use EzyArrayResponse to send arrays of data

    master

    The EzyArrayResponse interface is used to construct a response containing a collection or array of values. It follows a fluent API pattern where methods return this to allow chaining. You can add single objects, varargs, or any Iterable collection to the response data.

    // Adding a single value
    ezyArrayResponse.param("value1");
    
    // Adding multiple values via varargs
    ezyArrayResponse.params("value1", "value2", "value3");
    
    // Adding values from a collection/iterable
    List<String> list = Arrays.asList("a", "b");
    ezyArrayResponse.params(list);
  5. Use EzyObjectResponse to build object-based responses

    master

    When implementing or using object-based responses in EzyFox Server, use the EzyObjectResponse interface (implemented by EzyAbstractObjectResponse) to construct the data payload. You can attach additional parameters to the response or exclude specific keys from the underlying data object before it is marshalled.

    Key methods:

    • param(Object key, Object value): Adds a key-value pair to the response. These parameters are marshalled and added to the final EzyObject.
    • exclude(Object key): Specifies a key that should be removed from the response data before it is sent.

    Note that param and exclude return the response instance, allowing for a fluent API style.

    // Example of fluent usage for building a response
    EzyObjectResponse response = new MyObjectResponse(context, marshaller)
        .param("status", "success")
        .param("timestamp", System.currentTimeMillis())
        .exclude("internalSecretKey");
  6. Use EzyObjectResponse to send object data with parameters

    master

    The EzyObjectResponse interface is used to construct responses that contain key-value pairs. It allows you to attach data to a response using the param method and selectively remove data using the exclude method. Both methods return the EzyObjectResponse instance, enabling a fluent API style for building responses.

    // Example of building a response fluently
    ezyObjectResponse
        .param("userId", 12345)
        .param("status", "active")
        .exclude("internalId");
  7. Stream raw bytes using EzyStreamBytes

    master

    The EzyStreamBytes interface provides methods to broadcast or send raw byte arrays to one or more EzySession recipients via a specified EzyTransportType. If no transport type is provided, the implementation defaults to EzyTransportType.TCP.

    Use these methods when you need to push low-level binary data directly to clients without higher-level command encapsulation.

    // To a single recipient using default TCP
    ezyStreamBytes.execute(byteArray, recipientSession);
    
    // To multiple recipients using a specific transport type
    ezyStreamBytes.execute(byteArray, recipientCollection, EzyTransportType.UDP);
  8. Send a response to a child session using EzyChildSendResponse

    master

    The EzyChildSendResponse interface provides methods to send data (EzyData) to one or more child sessions (EzySession). You can specify whether the data should be encrypted and which transport type (EzyTransportType) to use. If no transport type is provided, it defaults to EzyTransportType.TCP.

    // Send to a single recipient via TCP (default)
    ezyChildSendResponse.execute(data, recipient, true);
    
    // Send to multiple recipients with specific transport and encryption
    ezyChildSendResponse.execute(data, recipients, false, EzyTransportType.UDP);
  9. Close a session using EzyCloseSession

    master

    The EzyCloseSession interface provides a mechanism to programmatically terminate an active EzySession. To close a session, call the close method, providing the target EzySession instance and an EzyConstant representing the reason for closure.

    // Implementation usage example
    EzyCloseSession closeSessionCommand = ...;
    EzySession session = ...;
    EzyConstant reason = EzyConstant.SOME_REASON;
    
    closeSessionCommand.close(session, reason);
  10. Implement and use EzyAbstractResponse for sending messages

    master

    EzyAbstractResponse is an abstract base class used to construct and dispatch messages (responses) to specific recipients within the EzyFox server. To use it, you must extend this class and implement the sendData(EzyData data, EzyTransportType transportType) method to define how the constructed data is actually transmitted.

    Key Capabilities

    • Command & Parameters: Set the message command name and attach EzyData parameters.
    • Recipient Management: Target specific EzySession objects, EzyUser objects, or usernames. You can use the exclude flag to treat recipients as exclusiveRecipients (to be removed from the final recipient list).
    • Encryption: Toggle whether the response should be encrypted using .encrypted(boolean).
    • Transport Control: Specify the EzyTransportType (e.g., TCP) for the delivery.
    • Execution Lifecycle: Calling .execute() calculates the final recipient list (subtracting exclusive recipients from the general recipients), builds the EzyData payload using the command and params, and triggers the sendData implementation.

    Recipient Logic

    When adding recipients, the exclude boolean determines the behavior:

    • exclude = false: Adds the session/user to the recipients set.
    • exclude = true: Adds the session/user to the exclusiveRecipients set.

    During .execute(), the final set of recipients is calculated as recipients.removeAll(exclusiveRecipients).

    // Example of a concrete implementation
    public class MyResponse extends EzyAbstractResponse<EzyZoneChildContext> {
        public MyResponse(EzyZoneChildContext context) {
            super(context);
        }
    
        @Override
        protected EzyUserManager getUserManager(EzyZoneChildContext context) {
            return context.getUserManager();
        }
    
        @Override
        protected void sendData(EzyData data, EzyTransportType transportType) {
            // Implementation to send data to the calculated recipients
            for (EzySession session : getRecipients()) {
                session.send(data, transportType);
            }
        }
    }
    
    // Usage pattern
    myResponse
        .command("login_success")
        .params(myData)
        .user(targetUser, false)
        .encrypted(true)
        .execute();