Handle EzyFox Server Events
EzyFox Server has some events:
- SERVER_INITIALIZING: fire when server is initializing, you can initialize some global configurations here
- SERVER_READY: fire when server ready, you can run something here (i.e batch job)
- USER_LOGIN: you need handle this event to authorize connected user
- USER_ACCESS_APP: you can handle this event of you want to authorize the use want to access the application
- USER_ADDED: fire when user has already added to server
- USER_REMOVED: fire when user has already remove from server or from an application
- SESSION_REMOVED: fire when a session has already remove from server or from an application
- 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<EzyServerReadyEvent> {
@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<EzyServerReadyEvent> {
@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<EzyUserLoginEvent> {
@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