Class DefaultGatewayClient
- java.lang.Object
-
- discord4j.gateway.DefaultGatewayClient
-
- All Implemented Interfaces:
GatewayClient
public class DefaultGatewayClient extends Object implements GatewayClient
Represents a Discord WebSocket client, called Gateway, implementing its lifecycle.Keeps track of a single websocket session by wrapping an instance of
GatewayWebsocketHandlereach 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 throughreceiver()and allows a producer to submit events throughsender().Provides sending raw
ByteBufpayloads throughsendBuffer(Publisher)and receiving rawByteBufpayloads mapped in-flight using a specified mapper usingreceiver(Function).
-
-
Constructor Summary
Constructors Constructor Description DefaultGatewayClient(GatewayOptions options)Initializes a new GatewayClient.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Mono<CloseStatus>close(boolean allowResume)Terminates this client's current gateway connection.Flux<discord4j.discordjson.json.gateway.Dispatch>dispatch()Obtains theFluxofDispatchevents inbound from the gateway connection made by this client.Mono<Void>execute(String gatewayUrl)Establish a reconnecting gateway connection to the given URL.DurationgetResponseTime()Gets the amount of time it last took Discord to respond to a heartbeat with an ack.intgetSequence()Gets the current heartbeat sequence.StringgetSessionId()Retrieve the ID of the current gateway session.intgetShardCount()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 theFluxof raw payloads inbound from the gateway connection made by this client.<T> Flux<T>receiver(Function<ByteBuf,Publisher<? extends T>> mapper)Obtains aFluxof 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 ofByteBufpayloads through thisGatewayClientand returns aMonothat signals completion when the given publisher completes.Sinks.Many<GatewayPayload<?>>sender()Retrieves a newSinks.Manyto safely produce outbound values usingSinks.Many.tryEmitNext(Object)orSinks.Many.emitNext(Object, Sinks.EmitFailureHandler).Flux<GatewayConnection.State>stateEvents()Return a sequence of theGatewayConnection.Statetransitions this client receives.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface discord4j.gateway.GatewayClient
send
-
-
-
-
Constructor Detail
-
DefaultGatewayClient
public DefaultGatewayClient(GatewayOptions options)
Initializes a new GatewayClient.- Parameters:
options- theGatewayOptionsto configure this client
-
-
Method Detail
-
execute
public Mono<Void> execute(String gatewayUrl)
Description copied from interface:GatewayClientEstablish a reconnecting gateway connection to the given URL.- Specified by:
executein interfaceGatewayClient- Parameters:
gatewayUrl- the URL used to establish a websocket connection- Returns:
- a
Monosignaling completion of the session. If a non-recoverable error terminates the session, it is emitted as an error through this Mono.
-
close
public Mono<CloseStatus> close(boolean allowResume)
Description copied from interface:GatewayClientTerminates this client's current gateway connection.- Specified by:
closein interfaceGatewayClient- Parameters:
allowResume- if resuming this session after closing is possible. if set totruethe main executionMonowill complete with aPartialDisconnectExceptionyou can use to perform additional behavior or reconnect.- Returns:
- a
Monodeferring completion until the disconnection has completed. If this client closed due to an error it is emitted through the Mono. If available, aCloseStatuswill be present.
-
dispatch
public Flux<discord4j.discordjson.json.gateway.Dispatch> dispatch()
Description copied from interface:GatewayClientObtains theFluxofDispatchevents 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()); });- Specified by:
dispatchin interfaceGatewayClient- Returns:
- a
FluxofDispatchvalues
-
receiver
public Flux<GatewayPayload<?>> receiver()
Description copied from interface:GatewayClientObtains theFluxof raw payloads inbound from the gateway connection made by this client.- Specified by:
receiverin interfaceGatewayClient- Returns:
- a
FluxofGatewayPayloadvalues
-
receiver
public <T> Flux<T> receiver(Function<ByteBuf,Publisher<? extends T>> mapper)
Description copied from interface:GatewayClientObtains aFluxof raw payloads inbound from the gateway connection made by this client, transformed by a mapping function.- Specified by:
receiverin interfaceGatewayClient- Type Parameters:
T- the type of the resulting inboundFlux- Parameters:
mapper- a mapping function turning rawByteBufobjects into a given type- Returns:
- a
Fluxof raw payloads transformed by a mapping function
-
sender
public Sinks.Many<GatewayPayload<?>> sender()
Description copied from interface:GatewayClientRetrieves a newSinks.Manyto safely produce outbound values usingSinks.Many.tryEmitNext(Object)orSinks.Many.emitNext(Object, Sinks.EmitFailureHandler).- Specified by:
senderin interfaceGatewayClient- Returns:
- a serializing
Sinks.Many
-
sendBuffer
public Mono<Void> sendBuffer(Publisher<ByteBuf> publisher)
Description copied from interface:GatewayClientSends a sequence ofByteBufpayloads through thisGatewayClientand returns aMonothat 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.
- Specified by:
sendBufferin interfaceGatewayClient- Parameters:
publisher- a sequence of outbound payloads- Returns:
- a
Monosignaling completion, if an error occurs while producing it is emitted through the Mono
-
getShardCount
public int getShardCount()
Description copied from interface:GatewayClientReturn number of shards this client operates under.- Specified by:
getShardCountin interfaceGatewayClient- Returns:
- a positive integer representing the number of shards
-
getSessionId
public String getSessionId()
Description copied from interface:GatewayClientRetrieve the ID of the current gateway session.- Specified by:
getSessionIdin interfaceGatewayClient- Returns:
- the ID of the current gateway session. Used for resuming and voice.
-
getSequence
public int getSequence()
Description copied from interface:GatewayClientGets the current heartbeat sequence.- Specified by:
getSequencein interfaceGatewayClient- Returns:
- an integer representing the current gateway sequence
-
stateEvents
public Flux<GatewayConnection.State> stateEvents()
Description copied from interface:GatewayClientReturn a sequence of theGatewayConnection.Statetransitions this client receives.- Specified by:
stateEventsin interfaceGatewayClient- Returns:
- a
Fluxof state elements
-
isConnected
public Mono<Boolean> isConnected()
Description copied from interface:GatewayClientReturns whether this GatewayClient is currently connected to Discord Gateway therefore capable to send and receive payloads.- Specified by:
isConnectedin interfaceGatewayClient- Returns:
- a
Monothat upon subscription, returns true if the gateway connection is currently established, false otherwise.
-
getResponseTime
public Duration getResponseTime()
Description copied from interface:GatewayClientGets the amount of time it last took Discord to respond to a heartbeat with an ack.- Specified by:
getResponseTimein interfaceGatewayClient- Returns:
- the duration which Discord took to respond to the last heartbeat with an ack.
-
-