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, wait
send
public 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 null
identifyLimiter
- rate-limiting policy used for IDENTIFY requests, allowing shard coordinationpublic Mono<Void> execute(String gatewayUrl)
GatewayClient
execute
in interface GatewayClient
gatewayUrl
- the URL used to establish a websocket connectionMono
signaling completionpublic Mono<Void> execute(String gatewayUrl, GatewayObserver additionalObserver)
GatewayClient
execute
in interface GatewayClient
gatewayUrl
- the URL used to establish a websocket connectionadditionalObserver
- an additional observer to be notified of eventsMono
signaling completionpublic Mono<Void> close(boolean allowResume)
GatewayClient
close
in interface GatewayClient
allowResume
- 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()
GatewayClient
Flux
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 GatewayClient
Flux
of Dispatch
valuespublic Flux<GatewayPayload<?>> receiver()
GatewayClient
Flux
of raw payloads inbound from the gateway connection made by this client.receiver
in interface GatewayClient
Flux
of GatewayPayload
valuespublic <T> Flux<T> receiver(Function<ByteBuf,Publisher<? extends T>> mapper)
GatewayClient
Flux
of raw payloads inbound from the gateway connection made by this client, transformed by a
mapping function.receiver
in interface GatewayClient
T
- the type of the resulting inbound Flux
mapper
- a mapping function turning raw ByteBuf
objects into a given typeFlux
of raw payloads transformed by a mapping functionpublic FluxSink<GatewayPayload<?>> sender()
GatewayClient
FluxSink
to safely produce outbound values using FluxSink.next(Object)
.sender
in interface GatewayClient
FluxSink
public Mono<Void> sendBuffer(Publisher<ByteBuf> publisher)
GatewayClient
ByteBuf
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 GatewayClient
publisher
- a sequence of outbound payloadsMono
signaling completion, if an error occurs while producing it is emitted through the Monopublic String getSessionId()
GatewayClient
getSessionId
in interface GatewayClient
public int getSequence()
GatewayClient
getSequence
in interface GatewayClient
public boolean isConnected()
GatewayClient
isConnected
in interface GatewayClient
public long getResponseTime()
GatewayClient
getResponseTime
in interface GatewayClient