-
Notifications
You must be signed in to change notification settings - Fork 6
The packet listener
So far we know about packets and how we create a packet class. So now it is time to understand how the communicating opponents know whether they received something and even distinguish what they received. The idea of a listener is very common and widely used, especially for networking. The purpose of a listener in this case is simply notifying the receiving opponent about a new packet.
There are 2 important classes for the usage of a listener. The first important class is the Packet listener class. This class functions as a wrapper class for the listener that are supposed to be registered. The next important class is the Packet handler annotation class. This annotation is used when you create your own listener class to tell Hydra about the methods that are intended to be used for a specific package.
We now want to actually use this wrapper class and the annotation. Here is a simple example of the usage of the wrapper class and the annotation.
@PacketHandler
public void sampleListenerMethod(ExamplePacket examplePacket, Session session) {
// Code here
}
In order to understand what the purpose of the annotation is we are going to look at the above example. The method here can be called however you want, the only important thing are the parameters. These have to be in this exact order (packet class first, followed by the session class). ExamplePacket is a class that extends the Packet class and is used to determine which method is supposed to be called after rebuilding the packet. The session parameter provides the session the packet was sent with. This for example comes handy in case you would like to send a specific answer to the opponent who send that specific packet. The last thing to remember is to use the wrapper class for the listener. This wrapper class allows you to register the listener to the protocol of Hydra, thus it is essential.
- Home
- Example usage of Hydra: Building a simple chat application
- Advanced example usage of Hydra: Building a small key-value store
- Example usage of Hydra's custom class serialization