Search in sources :

Example 1 with AsyncSSLSocketMiddleware

use of com.koushikdutta.async.http.AsyncSSLSocketMiddleware in project K6nele by Kaljurand.

the class WebSocketRecognitionService method startSocket.

/**
     * Opens the socket and starts recording/sending.
     *
     * @param url Webservice URL
     */
void startSocket(String url) {
    mIsEosSent = false;
    Log.i(url);
    AsyncHttpClient client = AsyncHttpClient.getDefaultInstance();
    if (false) {
        //http://stackoverflow.com/questions/37804816/androidasync-how-to-create-ssl-client-in-websocket-connection
        AsyncSSLSocketMiddleware sslSocketMiddleware = new AsyncSSLSocketMiddleware(client);
        SSLContext sslContext = null;
        try {
            sslContext = getSSLContext();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        }
        sslSocketMiddleware.setSSLContext(sslContext);
        client.insertMiddleware(sslSocketMiddleware);
    }
    client.websocket(url, PROTOCOL, new AsyncHttpClient.WebSocketConnectCallback() {

        @Override
        public void onCompleted(Exception ex, final WebSocket webSocket) {
            mWebSocket = webSocket;
            if (ex != null) {
                handleException(ex);
                return;
            }
            webSocket.setStringCallback(new WebSocket.StringCallback() {

                public void onStringAvailable(String s) {
                    Log.i(s);
                    handleResult(s);
                }
            });
            webSocket.setClosedCallback(new CompletedCallback() {

                @Override
                public void onCompleted(Exception ex) {
                    if (ex == null) {
                        Log.e("ClosedCallback");
                        handleFinish(mIsEosSent);
                    } else {
                        Log.e("ClosedCallback: ", ex);
                        handleException(ex);
                    }
                }
            });
            webSocket.setEndCallback(new CompletedCallback() {

                @Override
                public void onCompleted(Exception ex) {
                    if (ex == null) {
                        Log.e("EndCallback");
                        handleFinish(mIsEosSent);
                    } else {
                        Log.e("EndCallback: ", ex);
                        handleException(ex);
                    }
                }
            });
            startSending(webSocket);
        }
    });
}
Also used : AsyncSSLSocketMiddleware(com.koushikdutta.async.http.AsyncSSLSocketMiddleware) CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyManagementException(java.security.KeyManagementException) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) AsyncHttpClient(com.koushikdutta.async.http.AsyncHttpClient) WebSocket(com.koushikdutta.async.http.WebSocket)

Aggregations

CompletedCallback (com.koushikdutta.async.callback.CompletedCallback)1 AsyncHttpClient (com.koushikdutta.async.http.AsyncHttpClient)1 AsyncSSLSocketMiddleware (com.koushikdutta.async.http.AsyncSSLSocketMiddleware)1 WebSocket (com.koushikdutta.async.http.WebSocket)1 IOException (java.io.IOException)1 KeyManagementException (java.security.KeyManagementException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 CertificateException (java.security.cert.CertificateException)1 TimeoutException (java.util.concurrent.TimeoutException)1 SSLContext (javax.net.ssl.SSLContext)1