Another websocket client fix: the socket reference is not set soon enough + add some more error handling
This commit is contained in:
parent
95fa33a488
commit
87b9ffcc37
@ -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) {
|
||||||
|
// 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();
|
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) {
|
||||||
|
try {
|
||||||
AbstractClientWS.this.handleReceivedMessage(data.toString());
|
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) {
|
||||||
|
try {
|
||||||
AbstractClientWS.this.handleReceivedBinary();
|
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;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user