koush / AndroidAsync

Asynchronous socket, http(s) (client+server) and websocket library for android. Based on nio, not threads.
Other
7.52k stars 1.56k forks source link

Implementing an efficient retry policy for websocket #352

Open moradgholi opened 9 years ago

moradgholi commented 9 years ago

First of all thanks for this really awesome library.

I have 2 questions which all of them are related to each other so please tag this issue as question. 1- please look at this pseudo code :

public class MyApplication extends ApplicationClass {

    private WebSocket myWebsocket;
    private bool isDataSocketAvailable = false;

    public void setupWebScoketIfNeeded(){

        if (isDataSocketAvailable) return;
        AsyncHttpClient.getDefaultInstance().websocket("wss://example", "wss", new AsyncHttpClient.WebSocketConnectCallback() {
            @Override
            public void onCompleted(Exception ex, final WebSocket webSocket) {
                if (ex != null) {
                    // there is a connection problem so we have to retry with a new token
                    getNewAuthTokenAndConnectToServer();

                    ex.printStackTrace();
                    return;
                }

                Log.i("Socket", "Socket Connected Successfully");

                isDataSocketAvailable = true;
                // refrencing to new socket with myWebsocket
                myWebsocket = webSocket;

                myWebsocket.setStringCallback(new WebSocket.StringCallback() {
                    public void onStringAvailable(final String responseJson) {
                        parseServerEvent(responseJson);
                    }
                });

                myWebsocket.setClosedCallback(new CompletedCallback() {
                    @Override
                    public void onCompleted(Exception ex) {
                        isDataSocketAvailable = false;
                        // force close
                        myWebsocket.close();
                        // to help garbage collector
                        myWebsocket = null;
                        getNewAuthTokenAndConnectToServer();
                    }
                });
            }
        });
    }

    public void getNewAuthTokenAndConnectToServer() {
        // get a new token and then calling setupWebScoketIfNeeded();
    }
}

Is it safe to reconnect android async the close callback gets called ? At this time I see lots of alive connection on server which I think it's not usual. I think after setClosedCallback the websocket connection is still alive or it reconnects again and because of i'm creating multiple connection per client. I saw #235 and created a ping interval request for each 15 seconds to keep the connection alive.

2- It's very important to get the close error code which is referenced in #321, is there anyway to handle this ? I'm sending an error code when i'm closing a client connection to tell the token is not valid and the client should renew the token, as you can see in the code by default i'm getting a new token before initializing a new socket connection to make sure that the token is valid which is not standard.

Thanks

ofirant commented 8 years ago

Is there any solution regarding issue #321? I have exactly the same problem... Thanks

linuxokey commented 7 years ago

yes, i have a same problem. when i disconnect from server, i get a close callback. AND i'm sure there is no more re-connect in client code, 'cause i print log at all re-connect function(indeed only one connect function in my code). AND i'm put a breakpoint in sdk's connect function private void executeAffinity(final AsyncHttpRequest request, final int redirectCount, final FutureAsyncHttpResponse cancel, final HttpConnectCallback callback) but no trigger in that code.

BUT meantime i get a reconnect from niginx log with the same client.

i'm so confused. any ideas?