7

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();
            }
        });
    }
});
proximous
  • 617
  • 1
  • 10
  • 28

1 Answers1

5

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.

Community
  • 1
  • 1
Takahiko Kawasaki
  • 18,118
  • 9
  • 62
  • 105
  • Could you provide an example of how to call close() from outside of the WebSocketConnectCallback? I'm unclear how I would modify the sample code to perform the close. For example, if I define mWebSocketConnectCallback = new WebScokectConectCallback() {...}; mAsyncHttpClient = AsyncHttpClient.getDefaultInstance(); mAsyncHttpClient.websocket(mHostAddress, null, mWebSocketConnectCallback); can I use mAsyncHttpClient to close the connection? – proximous May 22 '15 at 03:23
  • 1
    I've not tried, but how about this? Future f = AsyncHttpClient.getDefaultInstance().websocket(...); WebSocket ws = f.get(); ws.close(); – Takahiko Kawasaki May 22 '15 at 05:34
  • That works for closing the WS. I'll still have to look at setClosedCallback() and setEndCallback() to really manage the connection. I'll also look at nv-websocket-client since managing my connection is critical so I'll see if that library offers a simple way for me do that. I really need a simple yet highly reliable means of creating, closing, and maintaining a websocket over an unreliable network link. Thanks! – proximous May 22 '15 at 16:08
  • I think your library is much easy to use and maintain. I was searching for information with the same issue. – March3April4 May 27 '16 at 16:54
  • Agree with @March3April4. Takahiko's library is much easier to implement and handles the websocket disconnect better. Also makes it easier to implement the callbacks. I'd suggest to migrate the library instead of trying to work around it. I spent about an hour troubleshooting and figuring out how to fix it, But took me 5 min to migrate to Takahiko's library. – velval Nov 07 '16 at 00:31