First, create a packet dispatcher for message processing. This dispatcher is used for the corresponding class, so be sure to generate static to prevent unnecessary waste of resources.
// (create packet dispatcher
private static final MessageDispatcher<MY_USER_CLASS> messageDispatcher = new MessageDispatcher<>();
Connect the desired message and handler to the dispatcher that you created.
// Mapping Handlers and Messages you want to process
static {
messageDispatcher.registerMsg(MyProto.MyMsg.class, _MyMsgHandler.class);
}
We now implement that message handler directly. At this time, GameAnvil uses naming that starts with __ for the message handler in all sample codes as well as inside the engine. In all of the example codes that appear in the future, all classes that begin with _ are message handlers. The most basic forms of message handlers are as follows. RECEVER_CLASS means the class that receives the message.
public class _MyGameMsg implements MessageHandler<RECEIVER_CLASS, MyProto.MyMsg> {
private static final Logger logger = getLogger(_MyMsgHandler.class);
@Override
public void execute(RECEIVER_CLASS receiver, MyProto.MyMsg myMsg) throws SuspendExecution {
}
}
For example, if the recipient of the packet is a GameUser, the message handler is as follows.
public class _MyGameMsg implements MessageHandler<GameUser, MyProto.MyMsg> {
private static final Logger logger = getLogger(_MyMsgHandler.class);
@Override
public void execute(GameUser receiver, MyProto.MyMsg myMsg) throws SuspendExecution {
}
}
We have implemented both packet dispatchers and message handlers. Now we just need to hand over the dispatcher when the engine has a packet to process.
public class GameUser {
// .. omitted
@Override
public final MessageDispatcher<GameUser> getMessageDispatcher() {
return packetDispatcher;
}
// .. omitted
}
So far, we've looked at the flow of general packet processing. We'll look at the processing of RESTful requests right away.
There are two types of packet dispatchers. One is a typical packet dispatcher we've shared earlier, and the other is a dispatcher for processing RESTful requests. Overall usage is about the same. However, this RESTful message processing supports only the SupportNode.
The following example code is creating a different RestPacketDispatcher to handle RESTful requests. It is also connecting an API in the form of a URL to the handler rather than a message class.
private static final RestMessageDispatcher<RECEIVER_CLASS> restDispatcher = new RestMessageDispatcher<>();
static {
//Restered with path and method(GET, POST, ...) .
restDispatcher.registerMsg("/auth", RestObject.GET, _RestAuthReq.class);
restDispatcher.registerMsg("/echo," RestObject.GET, _RestEchoReq.class);
restDispatcher.registerMsg("/testGET," RestObject.GET, _RestTestGET.class);
restDispatcher.registerMsg("/testPOST," RestObject.POST, _RestTestPOST.class);
}
As in the example code, RestMessageDispatcher is used to connect the RESTful API and the message handler that the user wants.
RESTful message handlers look the same except for the difference that RestObject is passed to the factor instead of Packet.
public class _RestAuthReq implements RestMessageHandler<WebSupportNode> {
private static final Logger logger = getLogger(_RestAuthReq.class);
@Override
public void execute(WebSupportNode node, RestObject restObject) throws SuspendExecution {
...
}
}
getRestMessageDispatcher for RESTful message processing supports only SupportNode. It can be easily implemented to pass the restMessageDispatcher.
@Override
public final RestMessageDispatcher<WebSupportNode> getRestMessageDispatcher() {
return restMessageDispatcher;
}