Search in sources :

Example 11 with AsyncHttpRequest

use of com.koushikdutta.async.http.AsyncHttpRequest 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

AsyncHttpRequest (com.koushikdutta.async.http.AsyncHttpRequest)11 Uri (android.net.Uri)4 AsyncHttpPost (com.koushikdutta.async.http.AsyncHttpPost)2 AsyncHttpResponse (com.koushikdutta.async.http.AsyncHttpResponse)2 StringBody (com.koushikdutta.async.http.body.StringBody)2 TimeoutException (java.util.concurrent.TimeoutException)2 CompletedCallback (com.koushikdutta.async.callback.CompletedCallback)1 AsyncHttpClient (com.koushikdutta.async.http.AsyncHttpClient)1 WebSocketConnectCallback (com.koushikdutta.async.http.AsyncHttpClient.WebSocketConnectCallback)1 AsyncHttpGet (com.koushikdutta.async.http.AsyncHttpGet)1 Multimap (com.koushikdutta.async.http.Multimap)1 WebSocket (com.koushikdutta.async.http.WebSocket)1 AsyncHttpRequestBody (com.koushikdutta.async.http.body.AsyncHttpRequestBody)1 HttpConnectCallback (com.koushikdutta.async.http.callback.HttpConnectCallback)1 LoaderEmitter (com.koushikdutta.ion.Loader.LoaderEmitter)1 IOException (java.io.IOException)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1