Search in sources :

Example 6 with WebSocket

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

WebSocket (com.koushikdutta.async.http.WebSocket)6 CompletedCallback (com.koushikdutta.async.callback.CompletedCallback)3 AsyncHttpClient (com.koushikdutta.async.http.AsyncHttpClient)2 WebSocketConnectCallback (com.koushikdutta.async.http.AsyncHttpClient.WebSocketConnectCallback)2 StringCallback (com.koushikdutta.async.http.WebSocket.StringCallback)2 AsyncHttpServer (com.koushikdutta.async.http.server.AsyncHttpServer)2 AsyncHttpServerRequest (com.koushikdutta.async.http.server.AsyncHttpServerRequest)2 IOException (java.io.IOException)2 Uri (android.net.Uri)1 FutureCallback (com.koushikdutta.async.future.FutureCallback)1 SimpleFuture (com.koushikdutta.async.future.SimpleFuture)1 AsyncHttpGet (com.koushikdutta.async.http.AsyncHttpGet)1 AsyncHttpRequest (com.koushikdutta.async.http.AsyncHttpRequest)1 AsyncSSLSocketMiddleware (com.koushikdutta.async.http.AsyncSSLSocketMiddleware)1 WebSocketRequestCallback (com.koushikdutta.async.http.server.AsyncHttpServer.WebSocketRequestCallback)1 SocketIOTransport (com.koushikdutta.async.http.socketio.transport.SocketIOTransport)1 WebSocketTransport (com.koushikdutta.async.http.socketio.transport.WebSocketTransport)1 XHRPollingTransport (com.koushikdutta.async.http.socketio.transport.XHRPollingTransport)1 KeyManagementException (java.security.KeyManagementException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1