From 0b20689e81e5f1836500f010a087d450427adcfb Mon Sep 17 00:00:00 2001 From: Andrew Guibert Date: Wed, 9 Oct 2019 17:51:57 -0500 Subject: [PATCH 1/2] Minimize pre-game playerlist spam --- .../java/org/libertybikes/game/core/GameRound.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/game-service/src/main/java/org/libertybikes/game/core/GameRound.java b/game-service/src/main/java/org/libertybikes/game/core/GameRound.java index 5848b9d..15b8972 100644 --- a/game-service/src/main/java/org/libertybikes/game/core/GameRound.java +++ b/game-service/src/main/java/org/libertybikes/game/core/GameRound.java @@ -76,6 +76,7 @@ public class GameRound implements Runnable { private final Deque playerRanks = new ArrayDeque<>(); private final Set lifecycleCallbacks = new HashSet<>(); private final int GAME_TICK_SPEED, MAX_TIME_BETWEEN_ROUNDS; + private volatile boolean broadcastPlayerList = true; private LobbyCountdown lobbyCountdown; private AtomicBoolean lobbyCountdownStarted = new AtomicBoolean(); @@ -151,6 +152,7 @@ public class GameRound implements Runnable { if (c == null) return false; c.player.ifPresent((p) -> p.setDirection(msg.direction)); + broadcastPlayerList = true; return true; } @@ -223,6 +225,7 @@ public class GameRound implements Runnable { public void addSpectator(Session s) { log("A spectator has joined."); + broadcastPlayerList = true; clients.put(s, new Client(s)); sendToClient(s, new OutboundMessage.PlayerList(getPlayers())); sendToClient(s, board); @@ -475,6 +478,7 @@ public class GameRound implements Runnable { private void broadcastPlayerList() { sendToClients(getNonMobileSessions(), new OutboundMessage.PlayerList(getPlayers())); + broadcastPlayerList = false; } private void checkForWinner() { @@ -591,7 +595,8 @@ public class GameRound implements Runnable { for (int i = 0; i < (STARTING_COUNTDOWN * 4); i++) { delay(250); - broadcastPlayerList(); + if (broadcastPlayerList) + broadcastPlayerList(); } paused.set(false); @@ -623,7 +628,8 @@ public class GameRound implements Runnable { while (isOpen() || gameState == State.FULL) { for (int i = 0; i < 4; i++) { delay(250); - broadcastPlayerList(); + if (broadcastPlayerList) + broadcastPlayerList(); } roundStartCountdown--; if (roundStartCountdown < 1) { From c9f148ce1572839406f319aa0b377a6191d542b8 Mon Sep 17 00:00:00 2001 From: Andrew Guibert Date: Wed, 9 Oct 2019 17:52:17 -0500 Subject: [PATCH 2/2] Send server messages async so disconnected clients do not hang the server --- .../game/round/service/GameRoundWebsocket.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/game-service/src/main/java/org/libertybikes/game/round/service/GameRoundWebsocket.java b/game-service/src/main/java/org/libertybikes/game/round/service/GameRoundWebsocket.java index d081797..11f08b6 100644 --- a/game-service/src/main/java/org/libertybikes/game/round/service/GameRoundWebsocket.java +++ b/game-service/src/main/java/org/libertybikes/game/round/service/GameRoundWebsocket.java @@ -5,6 +5,8 @@ package org.libertybikes.game.round.service; import java.io.IOException; import java.util.Set; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; import javax.enterprise.context.Dependent; import javax.inject.Inject; @@ -130,9 +132,10 @@ public class GameRoundWebsocket { public static void sendToClient(Session client, Object message) { if (client != null) { String msg = message instanceof String ? (String) message : jsonb.toJson(message); + Future f = client.getAsyncRemote().sendText(msg); try { - client.getBasicRemote().sendText(msg); - } catch (IOException e) { + f.get(50, TimeUnit.MILLISECONDS); + } catch (Exception e) { e.printStackTrace(); } }