Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close/retry/manage a WebSocket using Koush AndroidAsync?

I'm using Koush's AndroidAsync for a WebSocket client. My code follows the example at https://github.com/koush/AndroidAsync and works. (Example copied below.)

I need my app to open a websocket when it starts, however, I need to handle a few issues:

A) I need to allow the user to change the address of the websocket server. In this case, I need to close the existing websocket (which may have failed) and open a websocket to the new server.

B) The Server may be down or unavailable. In this case I'd like to report that back to the activity. Currently it simply silently fails.

So in order of importance:

  1. How do I close the websocket?
  2. How do I efficiently open a websocket to a new address? (Can I just reuse my AsyncHttpClient?)
  3. How do I retry on a failed or lost connection?
  4. How do I provide notification that the connection failed/closed?

If this is documented somewhere please let me know.

Example code from the website copied below:

AsyncHttpClient.getDefaultInstance().websocket(get,"my-protocol",new WebSocketConnectCallback(){
    @Override
    public void onCompleted(Exception ex,WebSocket webSocket){
        if(ex!=null){
            ex.printStackTrace();
            return;
        }
        webSocket.send("a string");
        webSocket.send(new byte[10]);
        webSocket.setStringCallback(new StringCallback(){
            public void onStringAvailable(String s){
                System.out.println("I got a string: "+s);
            }
        });
        webSocket.setDataCallback(new DataCallback(){
            public void onDataAvailable(ByteBufferList byteBufferList){
                System.out.println("I got some bytes!");
                // note that this data has been read
                byteBufferList.recycle();
            }
        });
    }
});
like image 750
proximous Avatar asked May 20 '15 03:05

proximous


People also ask

How to close WebSocket connection in Android?

Use WebSocket. close() for a graceful shutdown or cancel() for an immediate one.

How long does WebSocket connection stay open?

However, the connection between a client and your WebSocket app closes when no traffic is sent between them for 60 seconds.

When WebSocket connection is closed?

The WebSocket is closed before the connection is established error message indicates that some client code, or other mechanism, has closed the websocket connection before the connection was fully established.

Can Android use WebSockets?

The WebSocket protocol paved the way to a truly realtime web. At the time of writing this article, the Android SDK does not have native support for WebSockets. However, it relies on the Java Development Kit (JDK), which includes support for WebSockets as part of javax. websocket package.


1 Answers

I read the source code of AndroidAsync.

How to close

WebSocket interface inherits close() method from DataEmitter interface. Calling the close() method closes the WebSocket connection, but note that the implementation (WebSocketImpl.close()) does not perform the closing handshake which is required by RFC 6455.

Also, onDisconnect() in WebSocketImpl closes the underlying socket without performing the closing handshake when it receives a close frame.

So, in any case, the closing handshake is not performed. But, this is not a serious problem if you don't mind error logs on the server side.

How to retry & How to provide notifications

You may be able to detect disconnection by setting callbacks via setClosedCallback() method and setEndCallback() method, but I'm not sure.

How to retry and how to provide notifications are up to you. You can do as you like after you detect disconnection.

Recommendation

If you want to receive fine-grained events that occur on a WebSocket and want to know details about errors, try nv-websocket-client. Its listener interface has many callback entry points and it defines fine-grained error codes. The new WebSocket client library performs the closing handshake correctly.

like image 197
Takahiko Kawasaki Avatar answered Oct 24 '22 11:10

Takahiko Kawasaki