proulxs
proulxs

Reputation: 508

Atmosphere WASYNC Client (Reconnecting to downed server)

Software Stack:

We have two server instances in which one provides an endpoint using @ManagedService annotation and a second uses Wasync lib as the client to connect to it. The server client needs the connection to be persistent at all times and different use cases make this a bit challenging.

Our main problem use cases are:

  1. Connection dropped because of network outage or server is down.
  2. The client server is launched but the server endpoint isn't yet.

So we need a way to make the client dynamic in these situations.

IE:

  1. Websocket client is connected to endpoint.
  2. Server goes down for maintenance.
  3. Client tries to reconnect every xx seconds.
  4. Server starts back up properly.
  5. On next reconnect attempt, the client is connected.

Firstly, we've tried to use the built in reconnect options that Wasync provides with no dice:

OptionsBuilder<DefaultOptions, DefaultOptionsBuilder> optBuilder = wsAuthClient.newOptionsBuilder()
        .reconnect(true)
        .pauseBeforeReconnectInSeconds(10);

The listener:

}).on(Event.REOPENED, new Function<String>() {
        @Override
        public void on(String t) {
            logger.info("Re-opened connection to server.");
        }

Based on what I've read in the documentation, this gets triggered when the connection is purposely closed by the @ManagedService?

Has anyone encountered these types of use cases and have a solution for it?

Upvotes: 2

Views: 671

Answers (1)

proulxs
proulxs

Reputation: 508

I've solved this by using the @Get or @Post annotation on a method in the @ManagedService class endpoint. So my client polls this endpoint by doing an HTTP request, if it returns a 200 OK then this means the server is ready to accept connections.

SUDO:

while(!connected){
    //Do GET/POST to server to see if status is 200 OK.
    if (request.status == 200){
        connected = true;
        connectToWs();
    }
    // otherwise delay and re-attempt request.
}

Upvotes: 1

Related Questions