-
Notifications
You must be signed in to change notification settings - Fork 0
Events
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.
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");
});
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());
});