Compare commits

...

3 commits

Author SHA1 Message Date
0fd33af5fd discord-ws: Fix heartbeat timing
All checks were successful
ci/woodpecker/push/java Pipeline was successful
ci/woodpecker/push/oci-image-build Pipeline was successful
Wait til sending complets and then start the sleep.
2024-12-13 13:25:35 +01:00
7b0f88808d discord-ws: Close connection on reconect and invalid session 2024-12-13 13:22:22 +01:00
352eaa3e47 bot: Set logging level to DEBUG
All checks were successful
ci/woodpecker/push/java Pipeline was successful
ci/woodpecker/push/oci-image-build Pipeline was successful
We are not jet at a stable point.
2024-12-11 22:41:53 +01:00
2 changed files with 9 additions and 2 deletions

View file

@ -5,7 +5,7 @@
</encoder>
</appender>
<root level="INFO">
<root level="DEBUG">
<appender-ref ref="STDOUT"/>
</root>
</configuration>

View file

@ -15,6 +15,7 @@ import java.util.Optional;
import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
@ -89,11 +90,15 @@ public final class DiscordListener implements WebSocket.Listener {
case 7 -> {
logger.info("Reconnect request");
this.closeEventQueue.add(new CloseEvent.ResumableCloseEvent(this.resumeGatewayUrl.get(), this.sessionId.get(), lastSeq.get()));
webSocket.request(1);
return webSocket.sendClose(WebSocket.NORMAL_CLOSURE, "reconnect").thenAccept(WebSocket::abort);
}
// invalid session
case 9 -> {
logger.info("Invalid session");
this.closeEventQueue.add(new CloseEvent.UnresumableCloseEvent()); // it's technically possible to resume but unlikely -> https://discord.com/developers/docs/events/gateway#resuming
webSocket.request(1);
return webSocket.sendClose(WebSocket.NORMAL_CLOSURE, "invalid session").thenAccept(WebSocket::abort);
}
// hello event
case 10 -> init(webSocket, event.data());
@ -151,12 +156,14 @@ public final class DiscordListener implements WebSocket.Listener {
final String stringSeq = intSeq != 0 ? String.valueOf(intSeq) : "null";
// Send heartbeat
logger.debug("Sending heartbeat: {}", stringSeq);
webSocket.sendText("{\"op\": 1, \"d\": %s}".formatted(stringSeq), true);
webSocket.sendText("{\"op\": 1, \"d\": %s}".formatted(stringSeq), true).get();
receivedAck.set(false);
TimeUnit.MILLISECONDS.sleep(heartbeatInterval);
}
} catch (InterruptedException e) {
logger.error("Heartbeat interrupted", e);
} catch (ExecutionException e) {
logger.error("Heartbeat send failed", e);
}
logger.info("Stopping heartbeat");
});