socketio / socket.io-client-java

Full-featured Socket.IO Client Library for Java, which is compatible with Socket.IO v1.0 and later.
https://socketio.github.io/socket.io-client-java/installation.html
Other
5.32k stars 972 forks source link

sslContext is missing from the options #703

Closed Jonathanide1 closed 2 years ago

Jonathanide1 commented 2 years ago

This seems to have been removed in 1.x onwards - how are we supposed to set up an SSL context without it when we migrate to 1.x or later? Any help appreciated. BTW This is for an Android client that is migrating from 0.8x.

strongwill01 commented 2 years ago

try it.

// option is  IO.Options()
option.webSocketFactory = OKHttpClientTrustAll().getOKHttpClientTrustAll(OkHttpClient.Builder())
public OkHttpClient getOKHttpClientTrustAll(OkHttpClient.Builder builder) {
        builder.hostnameVerifier(new HostnameVerifier() {
            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });

        SSLContext sslcontext = null;

        try {
            sslcontext = SSLContext.getInstance("TLS");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        try {
            sslcontext.init(null, trustManagers, null);
        } catch (KeyManagementException e) {
            e.printStackTrace();
        }

        SSLEngine sslEngine = sslcontext.createSSLEngine();
        sslEngine.setUseClientMode(false);
        sslEngine.setNeedClientAuth(false);

        builder.sslSocketFactory(sslcontext.getSocketFactory(), new X509TrustManager() {
            @Override
            public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {

            }

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

            }

            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0];
            }
        });
        return builder.build();
    }

    TrustManager[] trustManagers = new TrustManager[]{
            new X509TrustManager() {
                @Override
                public void checkClientTrusted(X509Certificate[] chain, String authType) {
                }

                @Override
                public void checkServerTrusted(X509Certificate[] chain, String authType) {
                }

                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    return new X509Certificate[0];
                }
            }
    };
darrachequesne commented 2 years ago

For future readers:

Please check the documentation here: https://socketio.github.io/socket.io-client-java/initialization.html#SSL_connections

Please reopen if needed.