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
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 throughreceiver()
and allows a producer to submit events throughsender()
.Provides sending raw
ByteBuf
payloads throughsendBuffer(Publisher)
and receiving rawByteBuf
payloads 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 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.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.-
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
- theGatewayOptions
to configure this client
-
-
Method Detail
-
execute
public Mono<Void> execute(String gatewayUrl)
Description copied from interface:GatewayClient
Establish a reconnecting gateway connection to the given URL.- Specified by:
execute
in interfaceGatewayClient
- 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
public Mono<CloseStatus> close(boolean allowResume)
Description copied from interface:GatewayClient
Terminates this client's current gateway connection.- Specified by:
close
in interfaceGatewayClient
- 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
public Flux<discord4j.discordjson.json.gateway.Dispatch> dispatch()
Description copied from interface:GatewayClient
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()); });
- Specified by:
dispatch
in interfaceGatewayClient
- Returns:
- a
Flux
ofDispatch
values
-
receiver
public Flux<GatewayPayload<?>> receiver()
Description copied from interface:GatewayClient
Obtains theFlux
of raw payloads inbound from the gateway connection made by this client.- Specified by:
receiver
in interfaceGatewayClient
- Returns:
- a
Flux
ofGatewayPayload
values
-
receiver
public <T> Flux<T> receiver(Function<ByteBuf,Publisher<? extends T>> mapper)
Description copied from interface:GatewayClient
Obtains aFlux
of raw payloads inbound from the gateway connection made by this client, transformed by a mapping function.- Specified by:
receiver
in interfaceGatewayClient
- Type Parameters:
T
- the type of the resulting inboundFlux
- Parameters:
mapper
- a mapping function turning rawByteBuf
objects into a given type- Returns:
- a
Flux
of raw payloads transformed by a mapping function
-
sender
public Sinks.Many<GatewayPayload<?>> sender()
Description copied from interface:GatewayClient
Retrieves a newSinks.Many
to safely produce outbound values usingSinks.Many.tryEmitNext(Object)
orSinks.Many.emitNext(Object, Sinks.EmitFailureHandler)
.- Specified by:
sender
in interfaceGatewayClient
- Returns:
- a serializing
Sinks.Many
-
sendBuffer
public Mono<Void> sendBuffer(Publisher<ByteBuf> publisher)
Description copied from interface:GatewayClient
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.
- Specified by:
sendBuffer
in interfaceGatewayClient
- 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
public int getShardCount()
Description copied from interface:GatewayClient
Return number of shards this client operates under.- Specified by:
getShardCount
in interfaceGatewayClient
- Returns:
- a positive integer representing the number of shards
-
getSessionId
public String getSessionId()
Description copied from interface:GatewayClient
Retrieve the ID of the current gateway session.- Specified by:
getSessionId
in interfaceGatewayClient
- Returns:
- the ID of the current gateway session. Used for resuming and voice.
-
getSequence
public int getSequence()
Description copied from interface:GatewayClient
Gets the current heartbeat sequence.- Specified by:
getSequence
in interfaceGatewayClient
- Returns:
- an integer representing the current gateway sequence
-
stateEvents
public Flux<GatewayConnection.State> stateEvents()
Description copied from interface:GatewayClient
Return a sequence of theGatewayConnection.State
transitions this client receives.- Specified by:
stateEvents
in interfaceGatewayClient
- Returns:
- a
Flux
of state elements
-
isConnected
public Mono<Boolean> isConnected()
Description copied from interface:GatewayClient
Returns whether this GatewayClient is currently connected to Discord Gateway therefore capable to send and receive payloads.- Specified by:
isConnected
in interfaceGatewayClient
- Returns:
- a
Mono
that upon subscription, returns true if the gateway connection is currently established, false otherwise.
-
getResponseTime
public Duration getResponseTime()
Description copied from interface:GatewayClient
Gets the amount of time it last took Discord to respond to a heartbeat with an ack.- Specified by:
getResponseTime
in interfaceGatewayClient
- Returns:
- the duration which Discord took to respond to the last heartbeat with an ack.
-
-