Class 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 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 Detail

      • DefaultGatewayClient

        public DefaultGatewayClient​(GatewayOptions options)
        Initializes a new GatewayClient.
        Parameters:
        options - the GatewayOptions 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 interface GatewayClient
        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 interface GatewayClient
        Parameters:
        allowResume - if resuming this session after closing is possible. if set to true the main execution Mono will complete with a PartialDisconnectException 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, a CloseStatus will be present.
      • dispatch

        public Flux<discord4j.discordjson.json.gateway.Dispatch> dispatch()
        Description copied from interface: GatewayClient
        Obtains the Flux of Dispatch 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 interface GatewayClient
        Returns:
        a Flux of Dispatch values
      • receiver

        public <T> Flux<T> receiver​(Function<ByteBuf,​Publisher<? extends T>> mapper)
        Description copied from interface: GatewayClient
        Obtains a Flux of raw payloads inbound from the gateway connection made by this client, transformed by a mapping function.
        Specified by:
        receiver in interface GatewayClient
        Type Parameters:
        T - the type of the resulting inbound Flux
        Parameters:
        mapper - a mapping function turning raw ByteBuf objects into a given type
        Returns:
        a Flux of raw payloads transformed by a mapping function
      • sendBuffer

        public Mono<Void> sendBuffer​(Publisher<ByteBuf> publisher)
        Description copied from interface: GatewayClient
        Sends a sequence of ByteBuf payloads through this GatewayClient and returns a Mono 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 interface GatewayClient
        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 interface GatewayClient
        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 interface GatewayClient
        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 interface GatewayClient
        Returns:
        an integer representing the current gateway sequence
      • 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 interface GatewayClient
        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 interface GatewayClient
        Returns:
        the duration which Discord took to respond to the last heartbeat with an ack.