Class TwitchPubSub

java.lang.Object
com.github.twitch4j.pubsub.TwitchPubSub
All Implemented Interfaces:
ITwitchPubSub, AutoCloseable

public class TwitchPubSub extends Object implements ITwitchPubSub
Twitch PubSub
  • Field Details

    • REQUIRED_THREAD_COUNT

      public static final int REQUIRED_THREAD_COUNT
      See Also:
    • queueTask

      protected final Future<?> queueTask
      Command Queue Thread
    • heartbeatTask

      protected final Future<?> heartbeatTask
      Heartbeat Thread
    • isClosed

      protected volatile boolean isClosed
      is Closed?
    • commandQueue

      protected final BlockingQueue<String> commandQueue
      Command Queue
    • subscribedTopics

      protected final Set<PubSubRequest> subscribedTopics
      Holds the subscribed topics in case we need to reconnect
    • lastPing

      protected volatile long lastPing
      Last Ping send (1 minute delay before sending the first ping)
    • lastPong

      protected volatile long lastPong
      Last Pong received
    • taskExecutor

      protected final ScheduledExecutorService taskExecutor
      Thread Pool Executor
  • Constructor Details

    • TwitchPubSub

      public TwitchPubSub(WebsocketConnection websocketConnection, com.github.philippheuer.events4j.core.EventManager eventManager, ScheduledThreadPoolExecutor taskExecutor, ProxyConfig proxyConfig, Collection<String> botOwnerIds, int wsPingPeriod, IBackoffStrategy connectionBackoffStrategy, int wsCloseDelay, Consumer<PubSubResponsePayload> fallbackTopicHandler)
      Constructor
      Parameters:
      websocketConnection - WebsocketConnection
      eventManager - EventManager
      taskExecutor - ScheduledThreadPoolExecutor
      proxyConfig - ProxyConfig
      botOwnerIds - Bot Owner IDs
      wsPingPeriod - WebSocket Ping Period
      connectionBackoffStrategy - WebSocket Connection Backoff Strategy
      wsCloseDelay - Websocket Close Delay
      fallbackTopicHandler - Fallback Topic Handler
  • Method Details

    • connect

      public void connect()
      Connecting to IRC-WS
    • disconnect

      public void disconnect()
      Disconnecting from WebSocket
    • reconnect

      public void reconnect()
      Reconnecting to WebSocket
    • onPreConnect

      protected void onPreConnect()
    • onConnected

      protected void onConnected()
    • onTextMessage

      protected void onTextMessage(String text)
    • listenOnTopic

      public PubSubSubscription listenOnTopic(PubSubRequest request)
      Description copied from interface: ITwitchPubSub
      Send WS Message to subscribe to a topic
      Specified by:
      listenOnTopic in interface ITwitchPubSub
      Parameters:
      request - Topic
      Returns:
      PubSubSubscription to be listened to
    • unsubscribeFromTopic

      public boolean unsubscribeFromTopic(PubSubSubscription subscription)
      Description copied from interface: ITwitchPubSub
      Unsubscribe from a topic. Usage example:
            PubSubSubscription subscription = twitchPubSub.listenForCheerEvents(...);
            // ...
            twitchPubSub.unsubscribeFromTopic(subscription);
       
      Specified by:
      unsubscribeFromTopic in interface ITwitchPubSub
      Parameters:
      subscription - PubSubSubscription to be unlistened from
      Returns:
      whether the subscription was previously subscribed to
    • getLatency

      public long getLatency()
      Specified by:
      getLatency in interface ITwitchPubSub
      Returns:
      the most recently measured round-trip latency for the socket(s) in milliseconds, or -1 if unknown
    • close

      public void close()
      Close
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface ITwitchPubSub
    • getEventManager

      public com.github.philippheuer.events4j.core.EventManager getEventManager()
      EventManager
      Specified by:
      getEventManager in interface ITwitchPubSub