> On 18 Aug 2016, at 11:44, Rahman USTA <rahman.usta...@gmail.com> wrote:
> 
> Hmm, how should I refactor it on my code to make it clear?

Pardon, I don't think I fully understand the question.

In the API you have two _independent_ points where you can start your work.

1. When an instance of WebSocket is ready the CompletableFuture returned from
WebSocket.Builder.buildAsync completes with this instance.

2. When an instance of WebSocket is ready WebSocket.Listener.onOpen is invoked
with this instance.

These instances correspond to the same communication (or session/connection if
you like).

That's it. There's no guaranteed order in which these 2 events (obtaining an
instance) occur. By default the point where the initial request is made is
WebSocket.Listener.onOpen.

When and where you make consecutive requests usually depends on your readiness
to handle them. So for instance, if you were writing an echo server you might
decide to request the next message when the previous has been sent:

    CompletableFuture<?> onText(WebSocket webSocket, Text payload, boolean 
isLast) {
        
        return webSocket.sendText(payload, isLast)
                .thenRun(() -> webSocket.request(1));
    }

Just choose what suits you best, I guess.

Reply via email to