diff options
author | syeyoung <cyong06@naver.com> | 2021-05-15 17:34:01 +0900 |
---|---|---|
committer | syeyoung <cyong06@naver.com> | 2021-05-15 17:34:01 +0900 |
commit | e330634ad3a79cb6c010762509dbecfc2c587bce (patch) | |
tree | 976a17a4cdee88e2be9255f490f6116d24458e51 /src/main/java/kr/syeyoung/dungeonsguide/stomp | |
parent | 241e11397ba26ad337633fbd9a9e096795605f47 (diff) | |
download | Skyblock-Dungeons-Guide-e330634ad3a79cb6c010762509dbecfc2c587bce.tar.gz Skyblock-Dungeons-Guide-e330634ad3a79cb6c010762509dbecfc2c587bce.tar.bz2 Skyblock-Dungeons-Guide-e330634ad3a79cb6c010762509dbecfc2c587bce.zip |
heartbeats! and make it so that player can play even if server isn't loaded.
Diffstat (limited to 'src/main/java/kr/syeyoung/dungeonsguide/stomp')
-rw-r--r-- | src/main/java/kr/syeyoung/dungeonsguide/stomp/StompClient.java | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/src/main/java/kr/syeyoung/dungeonsguide/stomp/StompClient.java b/src/main/java/kr/syeyoung/dungeonsguide/stomp/StompClient.java index 1381a2c3..16943607 100644 --- a/src/main/java/kr/syeyoung/dungeonsguide/stomp/StompClient.java +++ b/src/main/java/kr/syeyoung/dungeonsguide/stomp/StompClient.java @@ -38,6 +38,10 @@ import java.security.cert.CertificateFactory; import java.security.cert.X509Certificate; import java.util.HashMap; import java.util.Map; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; public class StompClient extends WebSocketClient implements StompInterface { public StompClient(URI serverUri, final String token, CloseListener closeListener) throws Exception { @@ -61,11 +65,15 @@ public class StompClient extends WebSocketClient implements StompInterface { @Getter private StompPayload errorPayload; + private ScheduledFuture heartbeat = null; + + private static final ScheduledExecutorService ex = Executors.newScheduledThreadPool(1); @Override public void onOpen(ServerHandshake handshakedata) { send(new StompPayload().method(StompHeader.CONNECT) .header("accept-version","1.2") + .header("heart-beat", "30000,30000") .header("host",uri.getHost()).getBuilt() ); } @@ -76,6 +84,19 @@ public class StompClient extends WebSocketClient implements StompInterface { StompPayload payload = StompPayload.parse(message); if (payload.method() == StompHeader.CONNECTED) { stompClientStatus = StompClientStatus.CONNECTED; + + String heartbeat = payload.headers().get("heart-beat"); + if (heartbeat != null) { + int sx = Integer.parseInt(heartbeat.split(",")[0]); + int sy = Integer.parseInt(heartbeat.split(",")[1]); + + if (sy == 0) return; + int heartbeatMS = Integer.max(30000, sy); + this.heartbeat = ex.scheduleAtFixedRate(() -> { + send("\n"); + }, heartbeatMS-1000, heartbeatMS-1000, TimeUnit.MILLISECONDS); + } + } else if (payload.method() == StompHeader.ERROR) { errorPayload = payload; stompClientStatus = StompClientStatus.ERROR; @@ -113,6 +134,7 @@ public class StompClient extends WebSocketClient implements StompInterface { @Override public void onClose(int code, String reason, boolean remote) { + if (heartbeat != null) heartbeat.cancel(true); closeListener.onClose(code, reason, remote); } |