Another websocket client fix: the socket reference is not set soon enough + add some more error handling

This commit is contained in:
Marc Baloup 2023-03-20 23:10:52 +01:00
parent 95fa33a488
commit 87b9ffcc37

View File

@ -29,19 +29,37 @@ public abstract class AbstractClientWS implements AbstractWS {
private final Listener receiveListener = new Listener() { private final Listener receiveListener = new Listener() {
@Override @Override
public void onOpen(WebSocket webSocket) { public void onOpen(WebSocket webSocket) {
AbstractClientWS.this.onConnect(); // this method is actually called before the CompletableFuture from the WS Builder is completed, so
// we have to affect socket reference before doing anything
synchronized (socket) {
socket.set(webSocket);
isConnecting = false;
}
try {
AbstractClientWS.this.onConnect();
} catch (Exception e) {
logError("Error handling connection opening.", e);
}
Listener.super.onOpen(webSocket); Listener.super.onOpen(webSocket);
} }
@Override @Override
public CompletionStage<?> onText(WebSocket webSocket, CharSequence data, boolean last) { public CompletionStage<?> onText(WebSocket webSocket, CharSequence data, boolean last) {
AbstractClientWS.this.handleReceivedMessage(data.toString()); try {
AbstractClientWS.this.handleReceivedMessage(data.toString());
} catch (Exception e) {
logError("Error handling reception of text.", e);
}
return Listener.super.onText(webSocket, data, last); return Listener.super.onText(webSocket, data, last);
} }
@Override @Override
public CompletionStage<?> onBinary(WebSocket webSocket, ByteBuffer data, boolean last) { public CompletionStage<?> onBinary(WebSocket webSocket, ByteBuffer data, boolean last) {
AbstractClientWS.this.handleReceivedBinary(); try {
AbstractClientWS.this.handleReceivedBinary();
} catch (Exception e) {
logError("Error handling reception of binary.", e);
}
return Listener.super.onBinary(webSocket, data, last); return Listener.super.onBinary(webSocket, data, last);
} }
@ -105,6 +123,8 @@ public abstract class AbstractClientWS implements AbstractWS {
synchronized (socket) { synchronized (socket) {
isConnecting = false; isConnecting = false;
if (ws != null) { if (ws != null) {
// the value may already been set by the onOpen method of the receiveListener
// but just in case, we do it here too
socket.set(ws); socket.set(ws);
return; return;
} }