public class DefaultGatewayClient extends Object implements GatewayClient
Keeps track of a single websocket session by wrapping an instance of GatewayWebsocketHandler each time a
new WebSocket connection to Discord is made, therefore only one instance of this class is enough to
handle the lifecycle of the Gateway operations, that could span multiple WebSocket sessions over time.
Provides automatic reconnecting through a configurable retry policy, allows consumers to receive inbound events
through dispatch(), mapped payloads through receiver() and allows a producer to
submit events through sender().
Provides sending raw ByteBuf payloads through sendBuffer(Publisher) and receiving raw
ByteBuf payloads mapped in-flight using a specified mapper using receiver(Function).
| Constructor and Description |
|---|
DefaultGatewayClient(GatewayOptions options)
Initializes a new GatewayClient.
|
| Modifier and Type | Method and Description |
|---|---|
Mono<Void> |
close(boolean allowResume)
Terminates this client's current gateway connection.
|
Flux<discord4j.discordjson.json.gateway.Dispatch> |
dispatch()
Obtains the
Flux of Dispatch events inbound from the gateway connection made by this client. |
Mono<Void> |
execute(String gatewayUrl)
Establish a reconnecting gateway connection to the given URL.
|
Duration |
getResponseTime()
Gets the amount of time it last took Discord to respond to a heartbeat with an ack.
|
int |
getSequence()
Gets the current heartbeat sequence.
|
String |
getSessionId()
Retrieve the ID of the current gateway session.
|
int |
getShardCount()
Return number of shards this client operates under.
|
Mono<Boolean> |
isConnected()
Returns whether this GatewayClient is currently connected to Discord Gateway therefore capable to send and
receive payloads.
|
Flux<GatewayPayload<?>> |
receiver()
Obtains the
Flux of raw payloads inbound from the gateway connection made by this client. |
<T> Flux<T> |
receiver(Function<ByteBuf,Publisher<? extends T>> mapper)
Obtains a
Flux of raw payloads inbound from the gateway connection made by this client, transformed by a
mapping function. |
Mono<Void> |
sendBuffer(Publisher<ByteBuf> publisher)
Sends a sequence of
ByteBuf payloads through this GatewayClient and returns a Mono
that signals completion when the given publisher completes. |
FluxSink<GatewayPayload<?>> |
sender()
Retrieves a new
FluxSink to safely produce outbound values using FluxSink.next(Object). |
Flux<GatewayConnection.State> |
stateEvents()
Return a sequence of the
GatewayConnection.State transitions this client receives. |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitsendpublic DefaultGatewayClient(GatewayOptions options)
options - the GatewayOptions to configure this clientpublic Mono<Void> execute(String gatewayUrl)
GatewayClientexecute in interface GatewayClientgatewayUrl - the URL used to establish a websocket connectionMono signaling completion of the session. If a non-recoverable error terminates the session,
it is emitted as an error through this Mono.public Mono<Void> close(boolean allowResume)
GatewayClientclose in interface GatewayClientallowResume - if resuming this session after closing is possible. if set to true the main
execution Mono will complete with a PartialDisconnectException you can
use to perform additional behavior or reconnect.Mono deferring completion until the disconnection has completed. If this client closed due
to an error it is emitted through the Mono.public Flux<discord4j.discordjson.json.gateway.Dispatch> dispatch()
GatewayClientFlux of Dispatch events inbound from the gateway connection made by this client.
Can be used like this, for example, to get all created message events:
gatewayClient.dispatch().ofType(MessageCreate.class)
.subscribe(message -> {
System.out.println("Got a message with content: " + message.getMessage().getContent());
});
dispatch in interface GatewayClientFlux of Dispatch valuespublic Flux<GatewayPayload<?>> receiver()
GatewayClientFlux of raw payloads inbound from the gateway connection made by this client.receiver in interface GatewayClientFlux of GatewayPayload valuespublic <T> Flux<T> receiver(Function<ByteBuf,Publisher<? extends T>> mapper)
GatewayClientFlux of raw payloads inbound from the gateway connection made by this client, transformed by a
mapping function.receiver in interface GatewayClientT - the type of the resulting inbound Fluxmapper - a mapping function turning raw ByteBuf objects into a given typeFlux of raw payloads transformed by a mapping functionpublic FluxSink<GatewayPayload<?>> sender()
GatewayClientFluxSink to safely produce outbound values using FluxSink.next(Object).sender in interface GatewayClientFluxSinkpublic Mono<Void> sendBuffer(Publisher<ByteBuf> publisher)
GatewayClientByteBuf payloads through this GatewayClient and returns a Mono
that signals completion when the given publisher completes.
Sequences produced this way are not expected to be validated against errors or invalid input by the underlying implementation.
sendBuffer in interface GatewayClientpublisher - a sequence of outbound payloadsMono signaling completion, if an error occurs while producing it is emitted through the Monopublic int getShardCount()
GatewayClientgetShardCount in interface GatewayClientpublic String getSessionId()
GatewayClientgetSessionId in interface GatewayClientpublic int getSequence()
GatewayClientgetSequence in interface GatewayClientpublic Flux<GatewayConnection.State> stateEvents()
GatewayClientGatewayConnection.State transitions this client receives.stateEvents in interface GatewayClientFlux of state elementspublic Mono<Boolean> isConnected()
GatewayClientisConnected in interface GatewayClientMono that upon subscription, returns true if the gateway connection is currently
established, false otherwise.public Duration getResponseTime()
GatewayClientgetResponseTime in interface GatewayClient