From f3f616cdcaa27fac943fab768fde8837c1efb792 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Thu, 8 Jun 2023 12:07:49 +0200 Subject: [PATCH] Better detection of closed client side WS connection (to reconnect if necessary) --- .../lib/ws/client/AbstractClientWS.java | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/pandalib-ws-client/src/main/java/fr/pandacube/lib/ws/client/AbstractClientWS.java b/pandalib-ws-client/src/main/java/fr/pandacube/lib/ws/client/AbstractClientWS.java index c6a1014..33be6b9 100644 --- a/pandalib-ws-client/src/main/java/fr/pandacube/lib/ws/client/AbstractClientWS.java +++ b/pandalib-ws-client/src/main/java/fr/pandacube/lib/ws/client/AbstractClientWS.java @@ -159,18 +159,27 @@ public abstract class AbstractClientWS implements AbstractWS { @Override public final void sendString(String message) throws IOException { try { - synchronized (socket) { - WebSocket ws = socket.get(); - if (ws != null) - ws.sendText(message, true).join(); - else - throw new IOException("Connection is currently closed"); + try { + synchronized (socket) { + WebSocket ws = socket.get(); + if (ws != null) + ws.sendText(message, true).join(); + else + throw new IOException("Connection is currently closed"); + } + } catch (CompletionException ce) { + if (ce.getCause() instanceof IOException ioe) + throw ioe; + throw ThrowableUtil.uncheck(ce.getCause(), false); } - } catch (CompletionException ce) { - if (ce.getCause() instanceof IOException ioe) - throw ioe; - throw ThrowableUtil.uncheck(ce.getCause(), false); + } catch (IOException ioe) { + synchronized (socket) { + socket.set(null); + reconnectIfNecessary(); + } + throw ioe; } + } @Override