Handle Client Requests
Updated at 16998039710001. 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.EzyRequestController;
import com.tvd12.ezyfox.core.annotation.EzyDoHandle;
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
@EzyRequestController("chat")
public class ChatController {
@EzyAutoBind
private EzyResponseFactory appResponseFactory;
@EzyDoHandle("sendMessageToMe")
public Object sendMessageToMe(EzyUser user, SendMessageToOneRequest request) {
return new SendMessageResponse(user.getName(), request.getMessage());
}
@EzyDoHandle("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
}
@EzyDoHandle("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 to;
private String message;
}
}
You can find out full source code in Simple Chat Example You also can inject EzyAppContext, EzyUserSessionEvent and EzySession to your handler method like this:
@EzyDoHandle("sendMessageToOne")
public void sendMessageToOne(
EzyAppContext context, // or EzyContext
EzyUserSessionEvent event,
EzyUser user,
EzySession session,
SendMessageToOneRequest request
) {
// response
}
And if you don't need any component you can remove it, even all of them:
@EzyDoHandle("sendMessageToOne")
public void sendMessageToOne() {
// do something
}
When do we use EzyUser?
An user has many sessions, so you can use EzyUser when you need send a response to all sessions of the user. Example, in a chat application, your user has many device and each device is a EzySession, so when there is a message come, you will broadcast the message to all session, so you will use EzyUser.
When you just want to send response to exactly the requested session. Example, in a lucky wheel game, the request will come from a device, and you need response exactly result to that device, so you must use EzySession.
When do we use EzySession?
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
2. Handle Request By Singleton Handler
import static com.example.simple_chat.constant.Commands.HELLO;
import com.example.simple_chat.common.Greeting;
import com.tvd12.ezyfox.bean.annotation.EzyAutoBind;
import com.tvd12.ezyfox.bean.annotation.EzyPrototype;
import com.tvd12.ezyfox.binding.annotation.EzyObjectBinding;
import com.tvd12.ezyfox.core.annotation.EzyRequestListener;
import com.tvd12.ezyfox.function.EzyHandler;
import com.tvd12.ezyfox.util.EzyLoggable;
import com.tvd12.ezyfoxserver.context.EzyAppContext;
import com.tvd12.ezyfoxserver.context.EzyAppContextAware;
import com.tvd12.ezyfoxserver.entity.EzySession;
import com.tvd12.ezyfoxserver.entity.EzySessionAware;
import com.tvd12.ezyfoxserver.entity.EzyUser;
import com.tvd12.ezyfoxserver.entity.EzyUserAware;
import com.tvd12.ezyfoxserver.support.factory.EzyResponseFactory;
import lombok.Setter;
@Setter
@EzyPrototype
@EzyObjectBinding // @EzyArrayBinding
@EzyRequestListener(HELLO)
public class HelloRequestHandler
extends EzyLoggable
implements
EzyHandler,
EzyAppContextAware,
EzySessionAware,
EzyUserAware {
@EzyAutoBind
protected Greeting greeting;
@EzyAutoBind
protected EzyResponseFactory appResponseFactory;
protected EzyUser user;
protected EzySession session;
protected EzyAppContext appContext;
protected String who;
@Override
public void handle() {
appResponseFactory.newObjectResponse()
.command(HELLO)
.param("hello", greeting.greet(who))
.session(session)
.execute();
}
}
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 new 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.simple_chat.constant.Commands.HELLO;
import com.example.simple_chat.common.Greeting;
import com.tvd12.ezyfox.bean.annotation.EzyAutoBind;
import com.tvd12.ezyfox.bean.annotation.EzyPrototype;
import com.tvd12.ezyfox.binding.annotation.EzyObjectBinding;
import com.tvd12.ezyfox.core.annotation.EzyRequestListener;
import com.tvd12.ezyfox.function.EzyHandler;
import com.tvd12.ezyfox.util.EzyLoggable;
import com.tvd12.ezyfoxserver.context.EzyAppContext;
import com.tvd12.ezyfoxserver.context.EzyAppContextAware;
import com.tvd12.ezyfoxserver.entity.EzySession;
import com.tvd12.ezyfoxserver.entity.EzySessionAware;
import com.tvd12.ezyfoxserver.entity.EzyUser;
import com.tvd12.ezyfoxserver.entity.EzyUserAware;
import com.tvd12.ezyfoxserver.support.factory.EzyResponseFactory;
import lombok.Setter;
@Setter
@EzyPrototype
@EzyObjectBinding // @EzyArrayBinding
@EzyRequestListener(HELLO)
public class HelloRequestHandler
extends EzyLoggable
implements
EzyHandler,
EzyAppContextAware,
EzySessionAware,
EzyUserAware {
@EzyAutoBind
protected Greeting greeting;
@EzyAutoBind
protected EzyResponseFactory appResponseFactory;
protected EzyUser user;
protected EzySession session;
protected EzyAppContext appContext;
protected String who;
@Override
public void handle() {
appResponseFactory.newObjectResponse()
.command(HELLO)
.param("hello", greeting.greet(who))
.session(session)
.execute();
}
}
You can find out full source code in Hello World Example