Search in sources :

Example 46 with CompletedCallback

use of com.koushikdutta.async.callback.CompletedCallback in project simperium-android by Simperium.

the class AsyncWebSocketProvider method connect.

@Override
public void connect(final WebSocketManager.ConnectionListener listener) {
    Uri uri = Uri.parse(String.format(AndroidClient.WEBSOCKET_URL, mAppId));
    AsyncHttpRequest request = new AsyncHttpGet(uri);
    request.setHeader(AndroidClient.USER_AGENT_HEADER, mSessionId);
    // Protocol is null
    mAsyncClient.websocket(request, null, new WebSocketConnectCallback() {

        @Override
        public void onCompleted(Exception ex, final WebSocket webSocket) {
            if (ex != null) {
                listener.onError(ex);
                return;
            }
            if (webSocket == null) {
                listener.onError(new IOException("WebSocket could not be opened"));
                return;
            }
            final WebSocketManager.Connection connection = new WebSocketManager.Connection() {

                @Override
                public void close() {
                    mHandler.post(new Runnable() {

                        @Override
                        public void run() {
                            webSocket.close();
                        }
                    });
                }

                @Override
                public void send(final String message) {
                    mHandler.post(new Runnable() {

                        @Override
                        public void run() {
                            webSocket.send(message);
                        }
                    });
                }
            };
            webSocket.setStringCallback(new WebSocket.StringCallback() {

                @Override
                public void onStringAvailable(String s) {
                    listener.onMessage(s);
                }
            });
            webSocket.setEndCallback(new CompletedCallback() {

                @Override
                public void onCompleted(Exception ex) {
                    listener.onDisconnect(ex);
                }
            });
            webSocket.setClosedCallback(new CompletedCallback() {

                @Override
                public void onCompleted(Exception ex) {
                    listener.onDisconnect(ex);
                }
            });
            listener.onConnect(connection);
        }
    });
}
Also used : AsyncHttpGet(com.koushikdutta.async.http.AsyncHttpGet) CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) IOException(java.io.IOException) Uri(android.net.Uri) IOException(java.io.IOException) WebSocket(com.koushikdutta.async.http.WebSocket) WebSocketConnectCallback(com.koushikdutta.async.http.AsyncHttpClient.WebSocketConnectCallback) AsyncHttpRequest(com.koushikdutta.async.http.AsyncHttpRequest)

Aggregations

CompletedCallback (com.koushikdutta.async.callback.CompletedCallback)46 IOException (java.io.IOException)12 TimeoutException (java.util.concurrent.TimeoutException)11 ByteBufferList (com.koushikdutta.async.ByteBufferList)10 DataEmitter (com.koushikdutta.async.DataEmitter)10 DataCallback (com.koushikdutta.async.callback.DataCallback)10 Semaphore (java.util.concurrent.Semaphore)9 ContinuationCallback (com.koushikdutta.async.callback.ContinuationCallback)8 Continuation (com.koushikdutta.async.future.Continuation)8 CancellationException (java.util.concurrent.CancellationException)8 ExecutionException (java.util.concurrent.ExecutionException)8 AsyncHttpServer (com.koushikdutta.async.http.server.AsyncHttpServer)7 AsyncHttpServerRequest (com.koushikdutta.async.http.server.AsyncHttpServerRequest)7 FileNotFoundException (java.io.FileNotFoundException)7 AsyncHttpServerResponse (com.koushikdutta.async.http.server.AsyncHttpServerResponse)6 HttpServerRequestCallback (com.koushikdutta.async.http.server.HttpServerRequestCallback)6 AsyncSocket (com.koushikdutta.async.AsyncSocket)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 Uri (android.net.Uri)4 WritableCallback (com.koushikdutta.async.callback.WritableCallback)4