Compare commits

..

2 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

View file

@ -15,6 +15,7 @@ import java.util.Optional;
import java.util.Random; import java.util.Random;
import java.util.concurrent.BlockingQueue; import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CompletionStage; import java.util.concurrent.CompletionStage;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
@ -89,11 +90,15 @@ public final class DiscordListener implements WebSocket.Listener {
case 7 -> { case 7 -> {
logger.info("Reconnect request"); logger.info("Reconnect request");
this.closeEventQueue.add(new CloseEvent.ResumableCloseEvent(this.resumeGatewayUrl.get(), this.sessionId.get(), lastSeq.get())); 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 // invalid session
case 9 -> { case 9 -> {
logger.info("Invalid session"); 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 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 // hello event
case 10 -> init(webSocket, event.data()); 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"; final String stringSeq = intSeq != 0 ? String.valueOf(intSeq) : "null";
// Send heartbeat // Send heartbeat
logger.debug("Sending heartbeat: {}", stringSeq); 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); receivedAck.set(false);
TimeUnit.MILLISECONDS.sleep(heartbeatInterval); TimeUnit.MILLISECONDS.sleep(heartbeatInterval);
} }
} catch (InterruptedException e) { } catch (InterruptedException e) {
logger.error("Heartbeat interrupted", e); logger.error("Heartbeat interrupted", e);
} catch (ExecutionException e) {
logger.error("Heartbeat send failed", e);
} }
logger.info("Stopping heartbeat"); logger.info("Stopping heartbeat");
}); });