public class DefaultGatewayClient extends Object implements GatewayClient
Keeps track of a single websocket session by wrapping an instance of DiscordWebSocketHandler 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(HttpClient httpClient,
PayloadReader payloadReader,
PayloadWriter payloadWriter,
RetryOptions retryOptions,
String token,
IdentifyOptions identifyOptions,
GatewayObserver observer,
PayloadTransformer identifyLimiter)
Initializes a new GatewayClient.
|
| Modifier and Type | Method and Description |
|---|---|
Mono<Void> |
close(boolean allowResume)
Terminates this client's current gateway connection.
|
Flux<Dispatch> |
dispatch()
|
Mono<Void> |
execute(String gatewayUrl)
Establish a reconnecting gateway connection to the given URL.
|
Mono<Void> |
execute(String gatewayUrl,
GatewayObserver additionalObserver)
Establish a reconnecting gateway connection to the given URL, allowing an ad-hoc observer to be notified.
|
long |
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.
|
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). |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitsendpublic DefaultGatewayClient(HttpClient httpClient, PayloadReader payloadReader, PayloadWriter payloadWriter, RetryOptions retryOptions, String token, IdentifyOptions identifyOptions, @Nullable GatewayObserver observer, PayloadTransformer identifyLimiter)
httpClient - the underlying HttpClient used to perform the connectionpayloadReader - strategy to read and decode incoming gateway messagespayloadWriter - strategy to encode and write outgoing gateway messagesretryOptions - reconnect policy used in this clienttoken - Discord bot tokenidentifyOptions - used to IDENTIFY or RESUME a gateway connection, specifying the sharding options
and to set an initial presenceobserver - consumer observing gateway and underlying websocket lifecycle changes, can be nullidentifyLimiter - rate-limiting policy used for IDENTIFY requests, allowing shard coordinationpublic Mono<Void> execute(String gatewayUrl)
GatewayClientexecute in interface GatewayClientgatewayUrl - the URL used to establish a websocket connectionMono signaling completionpublic Mono<Void> execute(String gatewayUrl, GatewayObserver additionalObserver)
GatewayClientexecute in interface GatewayClientgatewayUrl - the URL used to establish a websocket connectionadditionalObserver - an additional observer to be notified of eventsMono signaling completionpublic 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.public Flux<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 String getSessionId()
GatewayClientgetSessionId in interface GatewayClientpublic int getSequence()
GatewayClientgetSequence in interface GatewayClientpublic boolean isConnected()
GatewayClientisConnected in interface GatewayClientpublic long getResponseTime()
GatewayClientgetResponseTime in interface GatewayClient