Socket.io client in android connection with HTTPS protocol failed?

Kotlin version :

    val myHostnameVerifier = HostnameVerifier { _, _ ->
        return@HostnameVerifier true
    }

    val trustAllCerts = arrayOf<TrustManager>(object : X509TrustManager
    {
        override fun checkClientTrusted(chain: Array<X509Certificate>, authType: String) {}

        override fun checkServerTrusted(chain: Array<X509Certificate>, authType: String) {}

        override fun getAcceptedIssuers(): Array<X509Certificate> {
            return arrayOf()
        }
    })

    val sslContext = SSLContext.getInstance("TLS")
    sslContext.init(null, trustAllCerts, null)

    val okHttpClient = OkHttpClient.Builder()
                                   .hostnameVerifier(myHostnameVerifier)
                                   .sslSocketFactory(sslContext.socketFactory, trustAllCerts[0] as X509TrustManager)
                                   .build()

    val options = IO.Options()
    options.transports = arrayOf(Polling.NAME)
    options.callFactory = okHttpClient
    options.webSocketFactory = okHttpClient

    val socket = IO.socket("https://...", options)
    socket.connect()

Thanks to @Apurva I resolve it on my own.

Seems that TrustManager is necessary to initialize sslContext

so I added

mySSLContext.init(null, trustAllCerts, null);

with parameter trustAllCerts refer to

private final TrustManager[] trustAllCerts= new TrustManager[] { new X509TrustManager() {
    public java.security.cert.X509Certificate[] getAcceptedIssuers() {
        return new java.security.cert.X509Certificate[] {};
    }

    public void checkClientTrusted(X509Certificate[] chain,
                                   String authType) throws CertificateException {
    }

    public void checkServerTrusted(X509Certificate[] chain,
                                   String authType) throws CertificateException {
    }
} };

And finally connected to chat server successfully.


Thanks to you @Dayo Choul, I got socket.io connection over https working. I was reading https://github.com/socketio/socket.io-client-java but it doesn't tell you everything you need. This post is still lacking these bits so I decided to post my code for anyone that might find it useful. Change ADDRESS and PORT to suit your needs.

HostnameVerifier myHostnameVerifier = new HostnameVerifier() {
    @Override
    public boolean verify(String hostname, SSLSession session) {
        return true;
    }
};
TrustManager[] trustAllCerts= new TrustManager[] { new X509TrustManager() {
    public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
    }

    public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
    }

    public X509Certificate[] getAcceptedIssuers() {
        return new X509Certificate[0];
    }
}};

SSLContext mySSLContext = null;
try {
    mySSLContext = SSLContext.getInstance("TLS");
    try {
        mySSLContext.init(null, trustAllCerts, null);
    } catch (KeyManagementException e) {
        e.printStackTrace();
    }
} catch (NoSuchAlgorithmException e) {
    e.printStackTrace();
}
OkHttpClient okHttpClient = new OkHttpClient.Builder().hostnameVerifier(myHostnameVerifier).sslSocketFactory(mySSLContext.getSocketFactory()).build();

// default settings for all sockets
IO.setDefaultOkHttpWebSocketFactory(okHttpClient);
IO.setDefaultOkHttpCallFactory(okHttpClient);

// set as an option
IO.Options opts = new IO.Options();
opts.callFactory = okHttpClient;
opts.webSocketFactory = okHttpClient;

socket = IO.socket("https://" + ADDRESS + ":PORT", opts);
socket.connect();