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:
- How do I close the websocket?
- How do I efficiently open a websocket to a new address? (Can I just reuse my AsyncHttpClient?)
- How do I retry on a failed or lost connection?
- 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();
}
});
}
});