GM Backend
This document is not mandatory reading. It introduces how other systems interact with ionet and is recommended when you have enough time.
Introduction
After reading this guide, developers can build their own GM backend that simulates user requests to access other logic servers.
The final goal is to make our GM backend support both HTTP communication and internal communication. Externally, it can provide HTTP for third-party integration; internally, it can interact with other logic servers and users.
We only need to start one logic server in the GM backend project to interact with ionet.
- External communication: provide HTTP APIs for third-party callbacks.
- Internal communication: interact with users in logic servers.
In this document, we use a web project to access actions, and call this web project the GM backend. The GM backend provides two interfaces:
- Recharge callback for a specified user.
- Broadcast message to all users on the server.
Overall flow: Access URL --> GM backend handles the HTTP request --> simulate a user request in a web MVC controller, and pass data to a logic-server action for processing --> then notify users inside the action.
Example Source Code
see https://github.com/iohao/ionet-examples
path : ionet-cookbook-code
- GameManagerApplication
- GameManagerAction
The Action below is used in the demo.
- code 6: broadcast to the current recharging user.
- code 12: broadcast to all users.
@ActionController(GameManagerCmd.cmd)
public final class GameManagerAction {
@ActionMethod(GameManagerCmd.recharge)
private long internalRecharge(long money, FlowContext flowContext) {
long userId = flowContext.getUserId();
flowContext.broadcastMe(broadcastUserLong, money);
return money;
}
@ActionMethod(GameManagerCmd.notice)
private void internalNotice(String message, FlowContext flowContext) {
flowContext.broadcastMulticast(broadcastMulticastString, message);
}
}
GM
The GM backend is a web project. The controller demonstrates how to call actions.
The recharge method performs user recharge through an action
- code 12: recharge amount.
- code 13: create
RequestMessage. - code 14: bind
userIdto simulate a user request. - code 15: request logic server via call.
Enter
http://localhost:8080/gm/rechargein a browser to triggerrecharge.
@RestController
@RequestMapping("gm")
public final class GameManagerController {
static final AtomicLong messageId = new AtomicLong();
static final long userId = 1378604058;
@GetMapping("/recharge")
public String recharge() {
var rechargeCmd = GameManagerCmd.of(GameManagerCmd.recharge);
var communication = CommunicationKit.getCommunication();
long money = RandomKit.random(1, 1000);
var requestMessage = communication.ofRequestMessage(rechargeCmd, money);
requestMessage.bindingUserId(userId);
var response = communication.call(requestMessage);
return "userId:%s, recharge %d".formatted(userId, response.getLong());
}
}
The notice method broadcasts to all users through an action
- code 10: message to broadcast.
- code 11: create
SendMessage. - code 12: request logic server via send.
Enter
http://localhost:8080/gm/noticein a browser to triggernotice.
@RestController
@RequestMapping("gm")
public final class GameManagerController {
...
@GetMapping("/notice")
public String notice() {
var noticeCmd = GameManagerCmd.of(GameManagerCmd.notice);
var communication = CommunicationKit.getCommunication();
String message = "GM web message " + messageId.incrementAndGet();
var sendMessage = communication.ofSendMessage(noticeCmd, message);
communication.send(sendMessage);
return "notice: " + message;
}
}
Summary
Now our GM backend supports both HTTP and internal communication. Externally, it can provide HTTP for third-party use; internally, it can interact with other logic servers and users. HTTP capability is provided by SpringMVC, and internal communication capability is provided by the GM logic server.
The GM backend is almost a required module for game servers. Its key values include:
- Receiving HTTP callbacks from external platforms (such as recharge callbacks).
- Communicating with internal logic servers and users.
- Supporting other auxiliary functions in the GM system.