Skip to content

ClientListeners

Ben edited this page Oct 24, 2022 · 1 revision

The ClientListener

Whenever something happens during the lifetime of the client, it probably fires a listener for it. You can catch these events by extending the ClientListener class and registering an instance of it on the client using client.registerListener(clientListener). Listeners can be added or removed dynamically during the lifetime of the client (they can be added after the client has been closed but I doubt it will ever fire any event - if it does: please open an issue and tell us how you achieved that!).

An example

Lets now try to print a message to the console once a connection has been established successfully.

import dev.bitbite.networking.ClientListener;

public class SuccessfulClientConnectionListener extends ClientListener {
	@Override
	public void onConnectionSuccess(){
		System.out.println("Connection has been established");
	}
}

Now you can instantiate the listener using var listener = new SuccessfulClientConnectionListener(); and register it on the client using client.registerListener(listener);. Now, once you run client.connect() and the connection is successful, the onConnectionSuccess() method will be called and the message is being printed.

Events

The following events may occur during the runtime of a client:

Client will try to connect to a server: onConnectionCreation();

Before the client even tries to connect, all ClientListeners will be notified.

Client successfully connected to a server: onConnectionSuccess();

Every time the connection we were trying to establish was sucessful, this method gets called. The simplest case to use this, would be to just log that the server actually accepted our connection and we can now talk to the server. You can of course use this method to fire other methods in your program like for example setting an online status on a user or something similar. Feel free to be creative!

Client fails to connect to a server: onConnectionFailed(Exception e);

When we try to establish a connection to a given server and it fails for whatever reason (maybe because the server is offline, you are offline or any other reason), a call to this method will be made and the Exception which causes the connection to fail is passed in. Its up to you if you use this method to retry to create a connection or just simply print the stacktrace to keep track of what went wrong.

Client is going to be closed: onCloseRequested();

If you have the client connected to a server, its good practise to disconnect before simply killing the program. You can do so by calling client.close(). Once the closing has been requested onCloseRequested() will be called, again even before the client tries to disconnect.

Client successfully disconnected: onCloseSuccess();

After calling client.close(); and letting it do all the nasty cleanup it needs to do, it probably also managed to disconnect cleanly from the server. If so, it will trigger onCloseSuccess();.

Client disconnection fails: onCloseFailed(Exception e);

If for whatever reason the server likes us so much, that it will not let us go, or something in the client went horribly wrong that we are not able to close the connection, the onCloseFailed(); method will be fired. Though this might mostly happen due to the connection already being closed...


Congratulations, you successfully made it to the end of this mess and should now be able to make use of listeners yourself.