-
Notifications
You must be signed in to change notification settings - Fork 6
Sessions
Sessions can generally be seen as connections between server and clients. They serve as reference for the connection and provide functionality.
Let's first take a look at the functionality the Session class provides. The most important method of the class is the send() method. This method lets you send a packet via the session to the connected opponent of the session. A session can just be active when there is an established connection between the two communicating opponents. To check this, the method isActive() shall be used. Please read the documentation of the Session class for more information about the other methods the class provides.
Usually you don't have to create a session instance by yourself. This is how you receive sessions:
// Assuming the instance 'client' is provided
Session clientSession = client.getSession();
/* Assuming the instance 'server' is provided
* and that a client is connected to the server,
* so the set 'getSessions()' returns is not empty
*/
Session serverSession = server.getSessions().get(0);
Another possibility to obtain the session is to use a SessionListener. Check out step three of the server setup to read how the session listener works.
Let's see how the funtionality of the Session class comes into play. Take a look at this example:
session.send(new SamplePacket("This is a message"));
We suppose an instance of the Session class is provided so we can send a packet via the session. We also suppose that the SamplePacket class exists. The send() method takes an instance of type Packet as parameter, so everything that derives from the Packet class can be send via the session. In this case we send the SamplePacket via this session. Check out the Creation of packets article for more information about how to create packets. This is going to be the next step.
- 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