How to reconnect a websocket on android?

0

I'm developing a App in Android Studio and I'm trying to get my websocket automatically re-connected in case the connection fails or it closes, but I can not find out how to do it, I hope you can help me .

    
asked by David Villegas 01.08.2018 в 21:37
source

1 answer

0

I found a method of reconnecting my websocket , to the moment in which a client is created for a specific connection, it is important to know that this client can not be reused when making a connection attempt of websocket , unless:

if(mWebSocketClient!=null){
        mWebSocketClient.getConnection().close();
        mWebSocketClient=null;
    }

Let's take our client and make it null, so that when reusing it, we can use in this case the same variable for the re-connection that would be as ideal and organized as possible. Then, the connection is simply restored, like this:

mWebSocketClient = new WebSocketClient(uri)

And implementing the respective methods, onOpen , onClose , onMessage , onError and onClosing for what you need the app .

That's how I solved my problem, I hope someone else works for it.

    
answered by 09.08.2018 в 21:30