Skip to content

Session

Thorben Kuck edited this page May 29, 2018 · 7 revisions

The Session

What is a Session

A Session is an Interface, which basically represents an Container for multiple things connected to a certain Client. Every ClientStart has an Representation as an Client, which contains such said Session. The Session is set, as the Client connects and shared throughout each Connection. Further is each Session unique. If you want, you can override the request a new Session for an Client, by calling:

Client client = ...
client.setup();

or provide an custom Session by stating:

Client client = ...
Session mySession = ...
client.setSession(mySession);

Both will produce an warning, when you override an existing Session.
Further: You can't provide null as an Session.


Accessing the Session

The Session can be accessed by several means. The most common way of accessing it, is via the CommunicationRegistration. You can get the Session of the current Client in the following fashion:

ServerStart serverStart = ...
CommunicationRegistration registration = serverStart.getCommunicationRegistration();
registration.register(TestObject.class).addFirst((session, testObject) -> ...);

If you choose 2 attributes, the second will be the session for the current ClientStart.

Further you can iterate over all clients in ServerStart.getClientList() or provide an ClientConnectedHandler and access the Session via Client.getSession().


Mechanims

The Session provides multiple functionalitys. Those are:

  • Events
    Which are detailed at the next Section.
  • Properties
    Each Session has an private set of Properties, which can be freely manipulated. You can access the Properties by stating Session.getProperties();. If you want, you can set a custom Properties-Object by stating Session.setProperties(Properties);. You do not need to provide certain Properties.
  • Identification
    A Session can be Identified. You set the value by stating Session.setIdentified(boolean); and read it by stating Session.isIdentified();. By Default this Value does nothing in particular, but you can use it, to secure your server using a login-system or something.
    Further an Session has an unique identifier. This is set at the creation of the Session and it is not recommendet to override this identifier. This Identifier is outdated. You may set it freely. Again, like the identified flag, this does nothing in particular
  • send
    By stating Session.send(Object) you can offer an Object to be send over the Network. It will be send over the DefaultConnection to the ClientStart which send the object you received.