Class DefaultGatewayClient
- All Implemented Interfaces:
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 Summary
ConstructorsConstructorDescriptionDefaultGatewayClient(GatewayOptions options) Initializes a new GatewayClient. -
Method Summary
Modifier and TypeMethodDescriptionclose(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.Establish a reconnecting gateway connection to the given URL.Gets the amount of time it last took Discord to respond to a heartbeat with an ack.intGets the current heartbeat sequence.Retrieve the ID of the current gateway session.intReturn number of shards this client operates under.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>Obtains aFluxof raw payloads inbound from the gateway connection made by this client, transformed by a mapping function.sendBuffer(Publisher<ByteBuf> publisher) Sends a sequence ofByteBufpayloads through thisGatewayClientand returns aMonothat signals completion when the given publisher completes.sender()Retrieves a newSinks.Manyto safely produce outbound values usingSinks.Many.tryEmitNext(Object)orSinks.Many.emitNext(Object, reactor.core.publisher.Sinks.EmitFailureHandler).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, waitMethods inherited from interface discord4j.gateway.GatewayClient
send
-
Constructor Details
-
DefaultGatewayClient
Initializes a new GatewayClient.- Parameters:
options- theGatewayOptionsto configure this client
-
-
Method Details
-
execute
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
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
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
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
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
Description copied from interface:GatewayClientRetrieves a newSinks.Manyto safely produce outbound values usingSinks.Many.tryEmitNext(Object)orSinks.Many.emitNext(Object, reactor.core.publisher.Sinks.EmitFailureHandler).- Specified by:
senderin interfaceGatewayClient- Returns:
- a serializing
Sinks.Many
-
sendBuffer
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
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
Description copied from interface:GatewayClientReturn a sequence of theGatewayConnection.Statetransitions this client receives.- Specified by:
stateEventsin interfaceGatewayClient- Returns:
- a
Fluxof state elements
-
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
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.
-