From 8976f763b2bbfb6a08128c2ebf4cb212f72eacf5 Mon Sep 17 00:00:00 2001 From: Sidd Date: Fri, 8 Jul 2022 15:06:48 -0500 Subject: [PATCH 1/2] fix: clear queue on pubsub reconnect --- .../src/main/java/com/github/twitch4j/pubsub/TwitchPubSub.java | 1 + 1 file changed, 1 insertion(+) 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..b528e0d36 100644 --- a/pubsub/src/main/java/com/github/twitch4j/pubsub/TwitchPubSub.java +++ b/pubsub/src/main/java/com/github/twitch4j/pubsub/TwitchPubSub.java @@ -257,6 +257,7 @@ public TwitchPubSub(WebsocketConnection websocketConnection, EventManager eventM spec.wsPingPeriod(wsPingPeriod); spec.onConnected(this::onConnected); spec.onTextMessage(this::onTextMessage); + spec.onPostDisconnect(commandQueue::clear); spec.taskExecutor(taskExecutor); spec.proxyConfig(proxyConfig); if (connectionBackoffStrategy != null) From 64205ba2b313cf117ce491cf439e5c57807ffbc8 Mon Sep 17 00:00:00 2001 From: Sidd Date: Fri, 8 Jul 2022 15:18:05 -0500 Subject: [PATCH 2/2] fix: reset pubsub ping timer in more cases --- .../java/com/github/twitch4j/pubsub/TwitchPubSub.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) 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 b528e0d36..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,6 +255,7 @@ 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); @@ -340,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(); } @@ -362,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);