Package discord4j.gateway
Interface GatewayClient
- All Known Implementing Classes:
DefaultGatewayClient
public interface GatewayClient
Represents a Discord real-time websocket client, called Gateway, implementing its lifecycle.
Allows consumers to receive inbound events through dispatch() and direct raw payloads through
receiver() and allows a producer to submit events through send(Publisher) and sender().
Additionally, supports low-level ByteBuf based communication through receiver(Function) and
sendBuffer(Publisher).
- See Also:
-
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.send(Publisher<? extends GatewayPayload<?>> publisher) Sends a sequence ofpayloadsthrough thisGatewayClientand returns aMonothat signals completion when the payloads have been sent.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.
-
Method Details
-
execute
Establish a reconnecting gateway connection to the given URL.- 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
Terminates this client's current gateway connection.- 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
Flux<discord4j.discordjson.json.gateway.Dispatch> dispatch()Obtains 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()); });- Returns:
- a
FluxofDispatchvalues
-
receiver
Flux<GatewayPayload<?>> receiver()Obtains theFluxof raw payloads inbound from the gateway connection made by this client.- Returns:
- a
FluxofGatewayPayloadvalues
-
receiver
Obtains aFluxof raw payloads inbound from the gateway connection made by this client, transformed by a mapping function. -
sender
Sinks.Many<GatewayPayload<?>> sender()Retrieves a newSinks.Manyto safely produce outbound values usingSinks.Many.tryEmitNext(Object)orSinks.Many.emitNext(Object, reactor.core.publisher.Sinks.EmitFailureHandler).- Returns:
- a serializing
Sinks.Many
-
send
Sends a sequence ofpayloadsthrough thisGatewayClientand returns aMonothat signals completion when the payloads have been sent.- Parameters:
publisher- a sequence of outbound payloads- Returns:
- a
Monocompleting when payloads have been sent
-
sendBuffer
Sends 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.
- Parameters:
publisher- a sequence of outbound payloads- Returns:
- a
Monosignaling completion, if an error occurs while producing it is emitted through the Mono
-
getShardCount
int getShardCount()Return number of shards this client operates under.- Returns:
- a positive integer representing the number of shards
-
getSessionId
String getSessionId()Retrieve the ID of the current gateway session.- Returns:
- the ID of the current gateway session. Used for resuming and voice.
-
getSequence
int getSequence()Gets the current heartbeat sequence.- Returns:
- an integer representing the current gateway sequence
-
stateEvents
Flux<GatewayConnection.State> stateEvents()Return a sequence of theGatewayConnection.Statetransitions this client receives.- Returns:
- a
Fluxof state elements
-
isConnected
Returns whether this GatewayClient is currently connected to Discord Gateway therefore capable to send and receive payloads.- Returns:
- a
Monothat upon subscription, returns true if the gateway connection is currently established, false otherwise.
-
getResponseTime
Duration getResponseTime()Gets the amount of time it last took Discord to respond to a heartbeat with an ack.- Returns:
- the duration which Discord took to respond to the last heartbeat with an ack.
-