Setup Embedded Server

Updated at 1690542941000
When you use embedded ezyfox-server you will not need use xml to settings. And programmatically is a better choice, you can feel free use any idea to apply your settings. Basically, ezyfox-server try to allow setting everything, but you usually want to setting something bellow, for the full example, you can look at here.

1. Setup socket

TCP socket is suitable for non-web application, so you should enable it. But if your server just serve only web, please set active = false And if you want to use UDP, you must enable TCP socket.

    EzySimpleSocketSetting socketSetting = new EzySocketSettingBuilder()
            .active(true) // active or not,  default true
            .address("0.0.0.0") // loopback address, default 0.0.0.0
            .codecCreator(MsgPackCodecCreator.class) // encoder/decoder creator, default MsgPackCodecCreator 
            .maxRequestSize(1024) // max request size, default 32768
            .port(3005) // port, default 3005
            .tcpNoDelay(true) // tcp no delay, default false
            .writerThreadPoolSize(8) // thread pool size for socket writer, default 8 
            .build();

2. Setup websocket

For web application, you must enable websocket, and json coder/encoder is a good choice.

    EzySimpleWebSocketSetting webSocketSetting = new EzyWebSocketSettingBuilder()
            .active(true) // active or not,  default true
            .address("0.0.0.0") // loopback address, default 0.0.0.0
            .codecCreator(JacksonCodecCreator.class) // encoder/decoder creator, default JacksonCodecCreator
            .maxFrameSize(1024) // max frame size, default 32768
            .port(2208) // port, default 2208
            .writerThreadPoolSize(8) // thread pool size for socket writer, default 8 
            .build();

3. Setup a Plugin

EzyFox Server will load EntryLoader and Entry first, so you need specific package to scan here. The packages to scan are which packages you contain component you want to register with bean management.

3.1 Create a PluginEntryLoader

    public static class HelloPluginEntry extends EzySimplePluginEntry {
        // packages to scan bean
        @Override
        protected String[] getScanablePackages() {
            return new String[] {
                    "com.tvd12.ezyfoxserver.embedded.test" // replace by your package
            };
        }
    }
    public static class HelloPluginEntryLoader extends EzyAbstractPluginEntryLoader {
        @Override
        public EzyPluginEntry load() throws Exception {
            return new HelloPluginEntry();
        }
    }

3.2 Build a PluginSeting

    EzySimplePluginSetting pluginSetting = new EzyPluginSettingBuilder()
            .name("hello") // plugin name
            .addListenEvent(EzyEventType.USER_LOGIN) // listen able events USER_LOGIN, USER_ADDED, USER_REMOVED
            .configFile("config.properties") 
            .entryLoader(HelloPluginEntryLoader.class)
    //      .entryLoaderArgs() // pass arguments to entry loader constructor
            .priority(1) // set priority, bigger number is lower, default 0
            .threadPoolSize(3) // set thread pool size to create executor service for this plugin, default 0
            .build();

4. Setup An Application

4.1 Create an AppEntryLoader

    public static class HelloAppEntry extends EzySimpleAppEntry {
        // packages to scan bean
        @Override
        protected String[] getScanablePackages() {
            return new String[] {
                    "com.tvd12.ezyfoxserver.embedded.test" // replace by your package
            };
        }
        @Override
        protected void setupBeanContext(EzyAppContext context, EzyBeanContextBuilder builder) {
            // register bean here
        }
    }
    public static class HelloAppEntryLoader extends EzyAbstractAppEntryLoader {
        @Override
        public EzyAppEntry load() throws Exception {
            return new HelloAppEntry();
        }
    }

4.2 Build an AppSetting

    EzySimpleAppSetting appSetting = new EzyAppSettingBuilder()
            .name("hello") // app's name
            .configFile("config.properties")
            .entryLoader(HelloAppEntryLoader.class)
            .maxUsers(9999) // set max user in this app, default 999999
    //      .entryLoaderArgs() // pass arguments to entry loader constructor
            .threadPoolSize(3) // set thread pool size to create executor service for this app, default 0
            .build();

5. Setup an UserManagement

An zone have only one UserManagement, so if you apply your setting here then every user in every app. and plugin will be applied.

    EzySimpleUserManagementSetting userManagementSetting = new EzyUserManagementSettingBuilder()
            .allowChangeSession(true) // allow change user's session, default true
            .allowGuestLogin(true) // allow guest login, default false
            .guestNamePrefix("Guest#") // set name prefix for guest
            .maxSessionPerUser(5) // set number of max sessions per user // default 5
            .userMaxIdleTimeInSecond(15) // set max idle time of an user, default 0
            .userNamePattern("^[a-z0-9_.]{3,36}$") // set username pattern, default ^[a-z0-9_.]{3,36}$
            .build();

In games, we often only allow one user to use one device at a time, so the settings for UserManagement will be as follows:

    EzySimpleUserManagementSetting userManagementSetting = new EzyUserManagementSettingBuilder()
            .allowChangeSession(true) // allow change user's session, default true
            .allowGuestLogin(false) // allow guest login, default false
            .guestNamePrefix("Guest#") // set name prefix for guest
            .maxSessionPerUser(1) // set number of max sessions per user // default 5
            .userMaxIdleTimeInSecond(0) // set max idle time of an user, default 0
            .userNamePattern("^[a-z0-9_.]{3,36}$") // set username pattern, default ^[a-z0-9_.]{3,36}$
            .build();

6. Setup a Zone

An ezyfox-server can have multiple zone, so depend your business, you can setting different from each other.

6.1 Build a ZoneSetting

    EzySimpleZoneSetting zoneSetting = new EzyZoneSettingBuilder()
            .name("hello") // zone's name
            .plugin(pluginSetting) // add a plug-in to zone
            .application(appSetting) // add an app to zone
            .configFile("config.properties") // set config file
            .maxUsers(999999) // set maximum user for zone
            .userManagement(userManagementSetting) // set user management settings
             // add event controller, accept SERVER_INITIALIZING, SERVER_READY
            .addEventController(EzyEventType.SERVER_READY, HelloZoneServerReadyController.class) 
            .build();

6.2 Create a ZoneEvenController

    public static class HelloZoneServerReadyController
            extends EzyAbstractZoneEventController {
        @Override
        public void handle(EzyZoneContext ctx, EzyServerReadyEvent event) {
            // add logic here
        }
    }

7. Setup a SessionManagement

An ezyfox-server have only one SessionManagement, so if you apply your setting here then every session in every zone, app. and plugin will be applied.

    EzySimpleMaxRequestPerSecond maxRequestPerSecond = new EzyMaxRequestPerSecondBuilder()
            .value(15) // max request in a second
            .action(EzyMaxRequestPerSecondAction.DROP_REQUEST) // action when get max
            .build();
    EzySimpleSessionManagementSetting sessionManagementSetting = new EzySessionManagementSettingBuilder()
            .sessionMaxIdleTimeInSecond(30) // set max idle time for session, default 30s
            .sessionMaxWaitingTimeInSecond(30) // set max waiting time to login for session, default 30s
            .sessionMaxRequestPerSecond(maxRequestPerSecond) // set max request in a session for a session
            .build();

8. Setup UDP

To use UDP, you must enable TCP socket, because in ezyfox-server we use TCP to authenticate. An UDP request must come from an authenticated tcp connecton.

    EzySimpleUdpSetting udpSetting = new EzyUdpSettingBuilder()
            .active(true) // active or not
            .address("0.0.0.0") // set loopback IP
            .channelPoolSize(16) // set number of udp channel for socket writing, default 16
            .codecCreator(MsgPackCodecCreator.class) // encoder/decoder creator, default MsgPackCodecCreator
            .handlerThreadPoolSize(5) // set number of handler's thread, default 5
            .maxRequestSize(1024) // set max request's size
            .port(2611) // set listen port
            .build();

9. Complete ServerSetting

9.1 Build ServerSetting

    EzySimpleSettings settings = new EzySettingsBuilder()
            .debug(true) // allow debug to print log or not, default false
            .nodeName("hello") // for convenient
            .zone(zoneSetting) // add a zone to server
            .socket(socketSetting) // set socket setting
            .websocket(webSocketSetting) // set websocket setting
            .udp(udpSetting) // set udp setting
            .sessionManagement(sessionManagementSetting) // set session management setting
            // add event controller, accept SERVER_INITIALIZING, SERVER_READY
            .addEventController(EzyEventType.SERVER_INITIALIZING, HelloServerReadyController.class)
            .build();

9.2 Create a ServerEvenController

    public static class HelloServerReadyController
            extends EzyAbstractServerEventController {
        @Override
        public void handle(EzyServerContext ctx, EzySimpleServerInitializingEvent event) {
            // add logic here
        }
    }

For the full example, you can look at here.

Next step

We can take a look list of ezyfox server's annotations.