request/response
介绍
我们将介绍单次请求处理的通信,单次请求处理有两种,分别是:
- request/response,请求/响应
- request/void,请求/无响应
tip
该通信模型比较强大,即可用于客户端的请求响应,又可用于逻辑服内部的请求响应。
request/response,请求/响应
request/response,请求/响应 是在服务器开发中常见的通信模型,客户端出请求后,服务器返回该请求的响应。
使用场景举例
装备的升级、人物的升级、玩家的移动、抽奖、游戏前端到某一个场景时需要从服务端获取一些对应的场景配置等。 在写 action 时,方法有返回值的就表示 request/response,请求/响应。
Example
action 在编码风格上,为开发者提供了类 MVC 的编码风格(无入侵的 Java Bean ),这种设计方式很好的避免了类爆炸。 返回值和参数支持自定义协议,int, long, bool, String, List 等类型,这使得开发者在业务的编写上更加的方便。
在学习成本方面非常低,可以说是零学习成本,即使没有网络编程经验也能轻松上手。 开发者只需掌握普通的 java 方法或 webMVC 相关知识,就能使用框架开发业务。
@ActionController(RequestCmd.cmd)
public class RequestAction {
AtomicInteger inc = new AtomicInteger(1);
@ActionMethod(RequestCmd.intAction)
private int intAction(int value) {
return value + inc.getAndIncrement();
}
@ActionMethod(RequestCmd.boolAction)
private boolean boolAction(boolean value) {
return inc.getAndIncrement() % 2 == 0;
}
@ActionMethod(RequestCmd.longAction)
private long longAction(long value) {
return value + inc.getAndIncrement();
}
@ActionMethod(RequestCmd.stringAction)
private String stringAction(String value) {
return value + inc.getAndIncrement();
}
@ActionMethod(RequestCmd.objectAction)
private BookMessage objectAction(AuthorMessage author) {
var book = new BookMessage();
book.bookName = "Social Psychology";
book.authorName = author.authorName;
return book;
}
@ActionMethod(RequestCmd.intListAction)
private List<Integer> intListAction(List<Integer> valueList) {
List<Integer> list = new ArrayList<>();
for (var value : valueList) {
list.add(value + inc.getAndIncrement());
}
return list;
}
@ActionMethod(RequestCmd.boolListAction)
private List<Boolean> boolListAction(List<Boolean> valueList) {
List<Boolean> list = new ArrayList<>();
for (var value : valueList) {
list.add(!value);
}
return list;
}
@ActionMethod(RequestCmd.longListAction)
private List<Long> longListAction(List<Long> valueList) {
List<Long> list = new ArrayList<>();
for (var value : valueList) {
list.add(value + inc.getAndIncrement());
}
return list;
}
@ActionMethod(RequestCmd.stringListAction)
private List<String> stringListAction(List<String> valueList) {
List<String> list = new ArrayList<>();
for (var value : valueList) {
list.add(value + inc.getAndIncrement());
}
return list;
}
@ActionMethod(RequestCmd.objectListAction)
private List<BookMessage> objectListAction(List<AuthorMessage> authorList) {
var book1 = new BookMessage();
book1.bookName = "Social Psychology";
book1.authorName = "David Myers";
var book2 = new BookMessage();
book2.bookName = "SThe Crowd: A Study of the Popular Mind";
book2.authorName = "Gustave Le Bon";
return List.of(book1, book2);
}
}
@ProtobufClass
public class AuthorMessage {
public String authorName;
}
request/void,请求/无响应
request/void,请求/无响应,当客户端发出请求后,服务器不会响应任何数据给请求端。 在写 action 时,将方法返回值声名为 void 就表示 request/void,请求/无响应
使用场景举例
如数据采集的场景、打点采集、日志传输、metrics 上报等。
Example
在写 action 时,将方法返回值声名为 void
@ActionController(RequestCmd.cmd)
public class RequestAction {
@ActionMethod(RequestCmd.emptyAction)
private void emptyAction() {
// your code
}
}
如何使用
关于处理客户端的请求处理
我们可以通过模拟客户端来发起请求。
关于内部的请求处理
小结
action 是 request/response 和 request/void 两种通信模型的结合体,可用于处理客户端请求和内部请求。