Skip to content

Commit

Permalink
fix: clear pubsub command queue on reconnect (#599)
Browse files Browse the repository at this point in the history
* fix: clear queue on pubsub reconnect

* fix: reset pubsub ping timer in more cases
  • Loading branch information
iProdigy committed Jul 9, 2022
1 parent b91e827 commit 7e44087
Showing 1 changed file with 8 additions and 4 deletions.
Expand Up @@ -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)
Expand Down Expand Up @@ -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();
}

Expand All @@ -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);

Expand Down

0 comments on commit 7e44087

Please sign in to comment.