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 throughreceiver()
and allows a producer to submit events throughsend(Publisher)
andsender()
.Additionally, supports low-level
ByteBuf
based communication throughreceiver(Function)
andsendBuffer(Publisher)
.- See Also:
- Gateway
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default 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 theFlux
ofDispatch
events inbound from the gateway connection made by this client.Mono<Void>
execute(String gatewayUrl)
Establish a reconnecting gateway connection to the given URL.Duration
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.int
getShardCount()
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 theFlux
of raw payloads inbound from the gateway connection made by this client.<T> Flux<T>
receiver(Function<ByteBuf,Publisher<? extends T>> mapper)
Obtains aFlux
of raw payloads inbound from the gateway connection made by this client, transformed by a mapping function.default Mono<Void>
send(Publisher<? extends GatewayPayload<?>> publisher)
Sends a sequence ofpayloads
through thisGatewayClient
and returns aMono
that signals completion when the payloads have been sent.Mono<Void>
sendBuffer(Publisher<ByteBuf> publisher)
Sends a sequence ofByteBuf
payloads through thisGatewayClient
and returns aMono
that signals completion when the given publisher completes.Sinks.Many<GatewayPayload<?>>
sender()
Retrieves a newSinks.Many
to safely produce outbound values usingSinks.Many.tryEmitNext(Object)
orSinks.Many.emitNext(Object, Sinks.EmitFailureHandler)
.Flux<GatewayConnection.State>
stateEvents()
Return a sequence of theGatewayConnection.State
transitions this client receives.
-
-
-
Method Detail
-
execute
Mono<Void> execute(String gatewayUrl)
Establish a reconnecting gateway connection to the given URL.- Parameters:
gatewayUrl
- the URL used to establish a websocket connection- Returns:
- a
Mono
signaling completion of the session. If a non-recoverable error terminates the session, it is emitted as an error through this Mono.
-
close
Mono<CloseStatus> close(boolean allowResume)
Terminates this client's current gateway connection.- Parameters:
allowResume
- if resuming this session after closing is possible. if set totrue
the main executionMono
will complete with aPartialDisconnectException
you can use to perform additional behavior or reconnect.- Returns:
- a
Mono
deferring completion until the disconnection has completed. If this client closed due to an error it is emitted through the Mono. If available, aCloseStatus
will be present.
-
dispatch
Flux<discord4j.discordjson.json.gateway.Dispatch> dispatch()
Obtains theFlux
ofDispatch
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()); });
- Returns:
- a
Flux
ofDispatch
values
-
receiver
Flux<GatewayPayload<?>> receiver()
Obtains theFlux
of raw payloads inbound from the gateway connection made by this client.- Returns:
- a
Flux
ofGatewayPayload
values
-
receiver
<T> Flux<T> receiver(Function<ByteBuf,Publisher<? extends T>> mapper)
Obtains aFlux
of raw payloads inbound from the gateway connection made by this client, transformed by a mapping function.
-
sender
Sinks.Many<GatewayPayload<?>> sender()
Retrieves a newSinks.Many
to safely produce outbound values usingSinks.Many.tryEmitNext(Object)
orSinks.Many.emitNext(Object, Sinks.EmitFailureHandler)
.- Returns:
- a serializing
Sinks.Many
-
send
default Mono<Void> send(Publisher<? extends GatewayPayload<?>> publisher)
Sends a sequence ofpayloads
through thisGatewayClient
and returns aMono
that signals completion when the payloads have been sent.- Parameters:
publisher
- a sequence of outbound payloads- Returns:
- a
Mono
completing when payloads have been sent
-
sendBuffer
Mono<Void> sendBuffer(Publisher<ByteBuf> publisher)
Sends a sequence ofByteBuf
payloads through thisGatewayClient
and returns aMono
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.
- Parameters:
publisher
- a sequence of outbound payloads- Returns:
- a
Mono
signaling 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.State
transitions this client receives.- Returns:
- a
Flux
of state elements
-
isConnected
Mono<Boolean> isConnected()
Returns whether this GatewayClient is currently connected to Discord Gateway therefore capable to send and receive payloads.- Returns:
- a
Mono
that 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.
-
-