diff --git a/pubsub/src/main/java/com/github/twitch4j/pubsub/TwitchPubSub.java b/pubsub/src/main/java/com/github/twitch4j/pubsub/TwitchPubSub.java index 4b984c24a..92168c49d 100644 --- a/pubsub/src/main/java/com/github/twitch4j/pubsub/TwitchPubSub.java +++ b/pubsub/src/main/java/com/github/twitch4j/pubsub/TwitchPubSub.java @@ -255,8 +255,10 @@ public TwitchPubSub(WebsocketConnection websocketConnection, EventManager eventM this.connection = new WebsocketConnection(spec -> { spec.baseUrl(WEB_SOCKET_SERVER); spec.wsPingPeriod(wsPingPeriod); + spec.onPreConnect(this::onPreConnect); spec.onConnected(this::onConnected); spec.onTextMessage(this::onTextMessage); + spec.onPostDisconnect(commandQueue::clear); spec.taskExecutor(taskExecutor); spec.proxyConfig(proxyConfig); if (connectionBackoffStrategy != null) @@ -339,10 +341,6 @@ public TwitchPubSub(WebsocketConnection websocketConnection, EventManager eventM * Connecting to IRC-WS */ public void connect() { - // Reset last ping to avoid edge case loop where reconnect occurred after sending PING but before receiving PONG - lastPong = TimeUtils.getCurrentTimeInMillis(); - lastPing = lastPong - 4 * 60 * 1000; - connection.connect(); } @@ -361,6 +359,12 @@ public void reconnect() { connection.reconnect(); } + protected void onPreConnect() { + // Reset last ping to avoid edge case loop where reconnect occurred after sending PING but before receiving PONG + lastPong = TimeUtils.getCurrentTimeInMillis(); + lastPing = lastPong - 4 * 60 * 1000; + } + protected void onConnected() { log.info("Connected to Twitch PubSub {}", WEB_SOCKET_SERVER);