Android Socket.io Websocket Transport does not works in SSL

前端 未结 3 1709
予麋鹿
予麋鹿 2020-12-19 21:00

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 o

相关标签:
3条回答
  • 2020-12-19 21:37

    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

     public WebsocketTransport(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 = new SocketIO();
    socket.setDefaultSSLSocketFactory(SSLContext.getDefault());
    socket.connect("https://www.myHttpsServer.com:443/");
    

    Hope it helps someone ;)

    0 讨论(0)
  • 2020-12-19 21:42

    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:

    OkHttpClient okHttpClient = new OkHttpClient.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 connection
    private Socket mSocket;
    
    // Configure options
    IO.Options options = new IO.Options();
    // ... add more options
    
    // End point https 
    String yourEndpoint = "https://whatever.yoururl.com"
    String yourHostName = "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 hostname
            HostnameVerifier hostnameVerifier = new HostnameVerifier() {
                @Override
                public boolean verify(String hostname, SSLSession session) {
                    HostnameVerifier hv = 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.

    0 讨论(0)
  • 2020-12-19 21:50

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

    0 讨论(0)
提交回复
热议问题