-
Notifications
You must be signed in to change notification settings - Fork 6
The protocol
In the chapters before we have learned what a packet is and how listeners are used to trigger events when packets are received. In this chapter we are going to learn about the protocol that keeps track of all registered packets and listeners. The protocol is an instance on the server and the client to specify the way in which these communicate.
In order to create a protocol that can be registered to a server or a client, one must create a class that extends the Hydra protocol class. This class provides methods for the registration of packets and listeners. Let's take a look at an easy example:
public class ExampleProtocol extends HydraProtocol {
public ExampleProtocol() {
// Register your packets and listener. This is a very important step!
// Otherwise Hydra can't work with them!
registerPacket(ExamplePacket.class);
registerListener(new ExamplePacketListener());
}
}
Here we have an example class that extends the Hydra protocol class. The parent class provides the methods registerPacket and registerListener. Go here to learn about listeners and how you create them. We use the constructor for the registration, because the constructor is guaranteed to be called when we pass an instance of our protocol class to the server or client.
There is just one thing left to do now. It's the simple step of passing the protocol to the server or client instance. This step is done, when you are using the builder of either the server or client. It's important to pass an instance of the protocol, so the server or client will be able to call the methods from the parent class, Hydra protocol.
- 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