Better detection of closed client side WS connection (to reconnect if necessary)

This commit is contained in:
Marc Baloup 2023-06-08 12:07:49 +02:00
parent 20643fec62
commit f3f616cdca

View File

@ -159,18 +159,27 @@ public abstract class AbstractClientWS implements AbstractWS {
@Override @Override
public final void sendString(String message) throws IOException { public final void sendString(String message) throws IOException {
try { try {
synchronized (socket) { try {
WebSocket ws = socket.get(); synchronized (socket) {
if (ws != null) WebSocket ws = socket.get();
ws.sendText(message, true).join(); if (ws != null)
else ws.sendText(message, true).join();
throw new IOException("Connection is currently closed"); 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) { } catch (IOException ioe) {
if (ce.getCause() instanceof IOException ioe) synchronized (socket) {
throw ioe; socket.set(null);
throw ThrowableUtil.uncheck(ce.getCause(), false); reconnectIfNecessary();
}
throw ioe;
} }
} }
@Override @Override