Handle Socket Events

Updated at 1742718415000

EzyFox Server Event Handling

EzyFox Server provides several server events that you can handle to customize server behavior. Below is a list of key events and their descriptions:
  1. SERVER_INITIALIZING: Triggered when the server is initializing. You can set up global configurations at this point.
  2. SERVER_READY: Triggered when the server is ready. You can perform tasks here, such as running batch jobs.
  3. USER_LOGIN: This event must be handled to authorize users when they connect to the server.
  4. USER_ACCESS_APP: Handle this event to authorize users attempting to access a specific application.
  5. USER_ADDED: Triggered when a user is successfully added to the server.
  6. USER_REMOVED: Triggered when a user is removed from the server or an application.
  7. SESSION_REMOVED: Triggered when a session is removed from the server or an application.
  8. STREAMING: Triggered when binary data is received from a client.

You can handle these events effortlessly by implementing appropriate event controllers.

1. Handling ServerReadyEvent in a 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. Handling ServerReadyEvent in an 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. Handling 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(event = EzyEventNames.USER_LOGIN)
public class HelloUserLoginController 
        extends EzyAbstractPluginEventController {

    @Override
    public void handle(EzyPluginContext ctx, EzyUserLoginEvent event) {
        logger.info("HELLO - user {} logged in", event.getUsername());
        if (event.getUsername().contains("admin")) {
            throw new EzyLoginErrorException(EzyLoginError.INVALID_USERNAME);
        }
    }
}

For more code examples, please refer to the Hello World project.

Next Steps

To proceed, you can learn how to handle client requests.

Table Of Contents