Handle Socket Events

Updated at 1685685310000

Handle EzyFox Server Events

EzyFox Server has some events:
  1. SERVER_INITIALIZING: fire when server is initializing, you can initialize some global configurations here
  2. SERVER_READY: fire when server ready, you can run something here (i.e batch job)
  3. USER_LOGIN: you need handle this event to authorize connected user
  4. USER_ACCESS_APP: you can handle this event of you want to authorize the use want to access the application
  5. USER_ADDED: fire when user has already added to server
  6. USER_REMOVED: fire when user has already remove from server or from an application
  7. SESSION_REMOVED: fire when a session has already remove from server or from an application
  8. STREAMING: fire when there is binary come from a client

And you can handle the that events so easily

1. Handle ServerReadyEvent on Plugin

    package com.tvd12.ezyfoxserver.embedded.test.plugin;
    import com.tvd12.ezyfox.bean.annotation.EzySingleton;
    import com.tvd12.ezyfox.core.annotation.EzyServerEventHandler;
    import com.tvd12.ezyfoxserver.constant.EzyEventNames;
    import com.tvd12.ezyfoxserver.context.EzyPluginContext;
    import com.tvd12.ezyfoxserver.controller.EzyAbstractPluginEventController;
    import com.tvd12.ezyfoxserver.event.EzyServerReadyEvent;
    @EzyEventHandler(event = EzyEventNames.SERVER_READY)
    public class HelloPluginServerReadyController 
            extends EzyAbstractPluginEventController {
        @Override
        public void handle(EzyPluginContext ctx, EzyServerReadyEvent event) {
            logger.info("HELLO PLUGIN - SERVER READY");
        }
    }

2. Handle ServerReadyEvent on App

    package com.tvd12.ezyfoxserver.embedded.test.app.controller;
    import com.tvd12.ezyfox.bean.annotation.EzySingleton;
    import com.tvd12.ezyfox.core.annotation.EzyServerEventHandler;
    import com.tvd12.ezyfoxserver.constant.EzyEventNames;
    import com.tvd12.ezyfoxserver.context.EzyAppContext;
    import com.tvd12.ezyfoxserver.controller.EzyAbstractAppEventController;
    import com.tvd12.ezyfoxserver.event.EzyServerReadyEvent;
    @EzyEventHandler(event = EzyEventNames.SERVER_READY)
    public class HelloAppServerReadyController 
            extends EzyAbstractAppEventController {
        @Override
        public void handle(EzyAppContext ctx, EzyServerReadyEvent event) {
            logger.info("HELLO APP - SERVER READY");
        }
    }

3. Handle UserLoginEvent

    package com.tvd12.ezyfoxserver.embedded.test.plugin;
    import com.tvd12.ezyfox.bean.annotation.EzySingleton;
    import com.tvd12.ezyfox.core.annotation.EzyServerEventHandler;
    import com.tvd12.ezyfoxserver.constant.EzyEventNames;
    import com.tvd12.ezyfoxserver.constant.EzyLoginError;
    import com.tvd12.ezyfoxserver.context.EzyPluginContext;
    import com.tvd12.ezyfoxserver.controller.EzyAbstractPluginEventController;
    import com.tvd12.ezyfoxserver.event.EzyUserLoginEvent;
    import com.tvd12.ezyfoxserver.exception.EzyLoginErrorException;
    @EzyEventHandler(EzyEventNames.USER_LOGIN)
    public class HelloUserLoginController 
            extends EzyAbstractPluginEventController {
        @Override
        public void handle(EzyPluginContext ctx, EzyUserLoginEvent event) {
            logger.info("HELLO - user {} login in", event.getUsername());
            if(event.getUsername().contains("admin"))
                throw new EzyLoginErrorException(EzyLoginError.INVALID_USERNAME);
        }
    }

You can take a look Hello World project to get more source code

Next step

We can handle client requests.