Skip to content
Tigran Sargsyan edited this page Sep 15, 2020 · 5 revisions

After that you are created client or server instance, you can listen some events.

For subscribing to events interface com.github.tix320.sonder.api.common.event.EventListener is used.

public interface EventListener {

	<T extends SonderEvent> Observable<T> on(Class<T> clazz);
}

Each event type is represented by some class subtype of com.github.tix320.sonder.api.common.event.SonderEvent.
You can use EventListener to listen any event by specifing the type.
The method returns Observable for handling events. If you familiar with reactive programming (e.g. RxJava), that's it.

For every client/server instance there are EventListener, which you can get via method getEventListener() in SonderClient and SonderServer.

Client side events

There are two events related to connection establishment and closing, ConnectionEstablishedEvent and ConnectionClosedEvent respectively.

SonderClient sonderClient = ...;

sonderClient.getEventListener().on(ConnectionEstablishedEvent.class).subscribe(connectionEstablishedEvent -> {
	System.out.println("Connected to server");
});


sonderClient.getEventListener().on(ConnectionClosedEvent.class).subscribe(connectionEstablishedEvent -> {
	System.out.println("Disconnected from server");
});

Server side events

There are two events related to client connection establishment and closing, NewClientConnectionEvent and ClientConnectionClosedEvent respectively.
Events are also contain the id of client.

SonderServer sonderServer = ...;

sonderServer.getEventListener().on(NewClientConnectionEvent.class).subscribe(event -> {
	System.out.printf("Client %s connected", event.getClientId());
});

sonderServer.getEventListener().on(ClientConnectionClosedEvent.class).subscribe(event -> {
	System.out.printf("Client %s disconnected", event.getClientId());
});

Next -> Protocols

Clone this wiki locally