0

This is my code of Socket Service. I want to use rejectUnauthorized: false, but this is available only for node, after findings I used TrustManager class to achieve this but it not working I am not able to connect to server, It always gives error

io.socket.engineio.client.EngineIOException: xhr poll error

 private void connectToSocket() {
    try {
        TrustManager[] trustAllCerts = new TrustManager[]{
                new X509TrustManager() {
                    public X509Certificate[] getAcceptedIssuers() {
                        return new X509Certificate[]{};
                    }
                    public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException {}

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

        // Create an SSL context with the custom trust manager
        SSLContext sslContext = SSLContext.getInstance("SSL");
        sslContext.init(null, trustAllCerts, new java.security.SecureRandom());


        // Set the SSL context on the OkHttpClient
        OkHttpClient client = new OkHttpClient.Builder()
                .sslSocketFactory(sslContext.getSocketFactory(), (X509TrustManager) trustAllCerts[0])
                .hostnameVerifier((hostname, session) -> true)
                .build();

        // Create Socket.IO options
        IO.Options options = new IO.Options();
        options.callFactory = (Call.Factory) client;
        options.webSocketFactory = (WebSocket.Factory) client;

        mSocket = IO.socket("https://127.0.0.1:5000", options);
        mSocket.on(Socket.EVENT_CONNECT, onConnect);
        mSocket.on(Socket.EVENT_DISCONNECT, onDisconnect);
        mSocket.on(Socket.EVENT_CONNECT_ERROR, onConnectError);
        mSocket.on(Constants.SOCKET_RECEIVE_COMMAND, onMessageReceived);
        mSocket.connect();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
James Z
  • 12,209
  • 10
  • 24
  • 44
userVani
  • 47
  • 4

0 Answers0