1. Handle Request By Controller
In this way, a controller is a singleton instance, it means a controller is created one time when application start up. It's proper to handle many request in a group
import java.util.Set;
import com.tvd12.ezyfox.bean.annotation.EzyAutoBind;
import com.tvd12.ezyfox.binding.annotation.EzyObjectBinding;
import com.tvd12.ezyfox.core.annotation.EzyClientRequestController;
import com.tvd12.ezyfox.core.annotation.EzyRequestHandle;
import com.tvd12.ezyfoxserver.entity.EzyUser;
import com.tvd12.ezyfoxserver.support.factory.EzyResponseFactory;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
@Setter
@EzyClientRequestController("chat")
public class ChatController {
@EzyAutoBind
private EzyResponseFactory appResponseFactory;
@EzyRequestHandle("sendMessageToOne")
public void sendMessageToOne(EzyUser user, SendMessageToOneRequest request) {
appResponseFactory.newObjectResponse()
.command("chat/sendMessage")
.data(new SendMessageResponse(user.getName(), request.getMessage()))
.username(request.getTo()) // send response to which user has that username
.execute(); // execute to send response
}
@EzyRequestHandle("sendMessageToGroup")
public void sendMessageToGroup(EzyUser user, SendMessageToOneRequest request) {
appResponseFactory.newObjectResponse()
.command("chat/sendMessage")
.data(new SendMessageResponse(user.getName(), request.getMessage()))
.usernames(request.getTo()) // send response to a collection of users
.execute(); // execute to send response
}
@Data
@EzyObjectBinding(write = false)
public static class SendMessageToOneRequest {
private String to;
private String message;
}
@Getter
@EzyObjectBinding(read = false)
@AllArgsConstructor
public static class SendMessageResponse {
private String from;
private String message;
}
@Data
@EzyObjectBinding(write = false)
public static class SendMessageToGroupRequest {
private Set<String> to;
private String message;
}
}
You can find out full source code in Simple Chat Example
2. Handle Request By Singleton Handler
In this way, a handler is a singleton instance, it means a handler is created one time when application start up. It's proper to handle a complicated request and you need a lot of Line of Codes to complete
import static com.example.simple_chat.constant.Commands.GREET;
import com.example.simple_chat.common.Greeting;
import com.example.simple_chat.handler.GreetRequestHandler.GreetRequest;
import com.tvd12.ezyfox.bean.annotation.EzyAutoBind;
import com.tvd12.ezyfox.bean.annotation.EzySingleton;
import com.tvd12.ezyfox.binding.annotation.EzyObjectBinding;
import com.tvd12.ezyfox.core.annotation.EzyClientRequestListener;
import com.tvd12.ezyfoxserver.context.EzyAppContext;
import com.tvd12.ezyfoxserver.event.EzyUserSessionEvent;
import com.tvd12.ezyfoxserver.support.factory.EzyResponseFactory;
import com.tvd12.ezyfoxserver.support.handler.EzyUserRequestHandler;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Setter;
@Setter
@EzySingleton
@EzyClientRequestListener(GREET)
public class GreetRequestHandler
implements EzyUserRequestHandler<EzyAppContext, GreetRequest> {
private String who;
@EzyAutoBind
private Greeting greeting;
@EzyAutoBind
private EzyResponseFactory appResponseFactory;
@Override
public void handle(
EzyAppContext context,
EzyUserSessionEvent event, GreetRequest data) {
appResponseFactory.newObjectResponse()
.command(GREET)
.data(new GreetResponse(greeting.greet(data.getWho())))
.param("message", greeting.greet(who))
.session(event.getSession())
.execute();
}
@Data
@EzyObjectBinding(write = false)
public static class GreetRequest {
private String who;
}
@Data
@AllArgsConstructor
@EzyObjectBinding(read = false)
public static class GreetResponse {
private String message;
}
}
You can find out full source code in Simple Chat Example
3. Handle Request By Prototype Handler
In this way, a handler is a prototype class, it means a handler is created when there is a request from an user. It's proper to handle a complicated request, when you need a lot of Line of Codes to complete and when you don't want to create a request class
import static com.example.hello_world.constant.Commands.GREET;
import com.example.hello_world.common.Greeting;
import com.tvd12.ezyfox.bean.annotation.EzyAutoBind;
import com.tvd12.ezyfox.bean.annotation.EzyPrototype;
import com.tvd12.ezyfox.binding.EzyDataBinding;
import com.tvd12.ezyfox.binding.annotation.EzyObjectBinding;
import com.tvd12.ezyfox.core.annotation.EzyClientRequestListener;
import com.tvd12.ezyfox.core.exception.EzyBadRequestException;
import lombok.Setter;
@Setter
@EzyPrototype
@EzyObjectBinding(write = false)
@EzyClientRequestListener(GREET)
public class GreetRequestHandler
extends ClientRequestHandler
implements EzyDataBinding {
private String who;
@EzyAutoBind
private Greeting greeting;
@Override
protected void execute() throws EzyBadRequestException {
responseFactory.newObjectResponse()
.command(GREET)
.param("message", greeting.greet(who))
.session(session)
.execute();
}
}
You can find out full source code in Hello World Example