Skip to content Skip to sidebar Skip to footer

Android Socket.io Websocket Transport Does Not Works In Ssl

I'm using gottox socket.io java client for an Android chat application. I could connect to both web-socket and Xhr transport in HTTP mode. But when i switch to HTTPS only Xhr mode

Solution 1:

Update

It might be that with new versions IO.setDefaultSSLContext and IO. setDefaultHostnameVerifier methods are not available. Instead now we can create our own OkHttpClient and set the hostname verifier and ssl socket factory etc on it as mentioned on socket.io-client-java usage. Here is the sniplet from there:

OkHttpClientokHttpClient=newOkHttpClient.Builder()
                      .hostnameVerifier(myHostnameVerifier)
                      .sslSocketFactory(mySSLContext.getSocketFactory(), myX509TrustManager)
                      .build(); // default settings for all sockets 

IO.setDefaultOkHttpWebSocketFactory(okHttpClient); 
IO.setDefaultOkHttpCallFactory(okHttpClient);

Initial Answer:

I had the same issue with io.socket:socket.io-client:0.7.0 version of socket.io library on Android for long. It used to work fine for http protocol, however for https protocol it had trouble establishing connection giving xhr poll errors.

Following solution works for me without modifying the library itself:

// Socket connectionprivate Socket mSocket;

// Configure options
IO.Optionsoptions=newIO.Options();
// ... add more options// End point https StringyourEndpoint="https://whatever.yoururl.com"StringyourHostName="yoururl.com"// If https, explicitly tell set the sslContext.if (yourEndpoint.startsWith("https://")) {        
    try {
        // Default settings for all sockets// Set default ssl context
        IO.setDefaultSSLContext(SSLContext.getDefault());

        // Set default hostnameHostnameVerifierhostnameVerifier=newHostnameVerifier() {
            @Overridepublicbooleanverify(String hostname, SSLSession session) {
                HostnameVerifierhv= HttpsURLConnection.getDefaultHostnameVerifier();
                return hv.verify(yourHostName, session);
            }
        };
        IO.setDefaultHostnameVerifier(hostnameVerifier);

        // set as an option
        options.sslContext = SSLContext.getDefault();
        options.hostnameVerifier = hostnameVerifier;
        options.secure = true;

    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
}

// Instantiate the socket
mSocket = IO.socket(mEndpoint, options);

Hope this helps.

Solution 2:

It works but you have to do some modifications on io.socket library. Instead of using the socketio.jar, import into src folder the io.socket library (You'll find inside socket.io-java-client package). There, you have to edit the WebsocketTransport class.

Here you have the solution

https://github.com/Gottox/socket.io-java-client/issues/60

publicWebsocketTransport(URI uri, IOConnection connection) {
    super(uri);
    this.connection = connection;
    SSLContext context = null;
    try {
        context = SSLContext.getInstance("TLS", "HarmonyJSSE");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchProviderException e) {
        e.printStackTrace();
    } 
    try {
        context.init(null, null, null);
    } catch (KeyManagementException e) {
        e.printStackTrace();
    }
    if("wss".equals(uri.getScheme()) && context != null) {
        this.setWebSocketFactory(new DefaultSSLWebSocketClientFactory(context));
    }
}

And remember to call the setDefaultSSLSocketFactory like this:

socket = newSocketIO();
socket.setDefaultSSLSocketFactory(SSLContext.getDefault());
socket.connect("https://www.myHttpsServer.com:443/");

Hope it helps someone ;)

Solution 3:

Websocket with SSL working in AndroidAsync. Using that for now.

Post a Comment for "Android Socket.io Websocket Transport Does Not Works In Ssl"