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:
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();
}
});
}
});
Use WebSocket. close() for a graceful shutdown or cancel() for an immediate one.
However, the connection between a client and your WebSocket app closes when no traffic is sent between them for 60 seconds.
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.
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.
I read the source code of AndroidAsync.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With