Search in sources :

Example 1 with WebSocketConnectCallback

use of com.koushikdutta.async.http.AsyncHttpClient.WebSocketConnectCallback in project AndroidAsync by koush.

the class WebSocketTests method testWebSocket.

public void testWebSocket() throws Exception {
    final Semaphore semaphore = new Semaphore(0);
    AsyncHttpClient.getDefaultInstance().websocket("http://localhost:5000/ws", null, new WebSocketConnectCallback() {

        @Override
        public void onCompleted(Exception ex, WebSocket webSocket) {
            webSocket.send("hello");
            webSocket.setStringCallback(new StringCallback() {

                @Override
                public void onStringAvailable(String s) {
                    assertEquals(s, "hello");
                    semaphore.release();
                }
            });
        }
    });
    assertTrue(semaphore.tryAcquire(TIMEOUT, TimeUnit.MILLISECONDS));
}
Also used : WebSocketConnectCallback(com.koushikdutta.async.http.AsyncHttpClient.WebSocketConnectCallback) StringCallback(com.koushikdutta.async.http.WebSocket.StringCallback) Semaphore(java.util.concurrent.Semaphore) WebSocket(com.koushikdutta.async.http.WebSocket)

Example 2 with WebSocketConnectCallback

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

WebSocketConnectCallback (com.koushikdutta.async.http.AsyncHttpClient.WebSocketConnectCallback)2 WebSocket (com.koushikdutta.async.http.WebSocket)2 Uri (android.net.Uri)1 CompletedCallback (com.koushikdutta.async.callback.CompletedCallback)1 AsyncHttpGet (com.koushikdutta.async.http.AsyncHttpGet)1 AsyncHttpRequest (com.koushikdutta.async.http.AsyncHttpRequest)1 StringCallback (com.koushikdutta.async.http.WebSocket.StringCallback)1 IOException (java.io.IOException)1 Semaphore (java.util.concurrent.Semaphore)1